许明会的计算机技术主页

Language:C,C++,.NET Framework(C#)
Thinking:Design Pattern,Algorithm,WPF,Windows Internals
Database:SQLServer,Oracle,MySQL,PostSQL
IT:MCITP,Exchange,Lync,Virtualization,CCNP

导航

TCP编程(6):客户端接收程序TcpClient

/*--===------------------------------------------===---
客户端接收程序:
    TcpClient, NetworkStream

            许明会    2007年12月9日 22:57:13
--===------------------------------------------===---
*/
using System;
using System.Collections.Generic;
using System.Text;

namespace NetCommunication
{
    
class tcpClient
    {
        
private const int port = 7788;
        
private const string hostname = "127.0.0.1";

        
static void Main()
        {
            
try
            {
                Console.WriteLine(
"尝试连接服务器{0}:{1}\t{2}", hostname, port, DateTime.Now.ToString());
                System.Net.Sockets.TcpClient tc 
= new System.Net.Sockets.TcpClient(hostname, port);
                System.Net.Sockets.NetworkStream ns 
= tc.GetStream();
                
byte[] bytes = new byte[1024];
                
int nCount = ns.Read(bytes, 0, bytes.Length);
                
if(nCount>0)
                    Console.WriteLine(System.Text.Encoding.ASCII.GetString(bytes, 
0, nCount));
                tc.Close();
                Console.ReadLine();
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

posted on 2007-12-09 23:58  许明会  阅读(987)  评论(0编辑  收藏  举报