【C#食谱】【杭帮菜】菜单2:写一个TCP客户端

问题:
你想连接基于TCP的服务端。
解决方法:
使用 System.Net.TcpClient类,通过给服务端传递地址和端口来和服务端建立连接和会话。下面这个例子将和上一菜单中的服务端进行会话。
class  MyTcpClient
{
    
private TcpClient _client = null;
    
private IPAddress _address;
    
private int _port;
    
private IPEndPoint _endPoint = null;

    
public MyTcpClient(IPAddress address, int port)
    
{
        _address 
= address;
        _port 
= port;
        _endPoint 
= new IPEndPoint(_address, _port);
    }



    
public void ConnectToServer(string msg)
    
{
        
try
        
{
            _client 
= new TcpClient();
            _client.Connect(_endPoint);

            
// 获取将要发生的消息
            byte[] bytes = Encoding.ASCII.GetBytes(msg);
            
// 获取和服务端会话的流
            using (NetworkStream ns = _client.GetStream())
            
{
                
// 发送消息
                Trace.WriteLine("Sending message to server: " + msg);
                ns.Write(bytes, 
0, bytes.Length);
                
// 获得响应
                
// 存储响应内容的缓存
                bytes = new byte[1024];

                
// 显示响应内容
                int bytesRead = ns.Read(bytes, 0, bytes.Length);
                
string serverResponse = Encoding.ASCII.GetString(bytes, 0, bytesRead);
                Trace.WriteLine(
"Server said: " + serverResponse);
            }

        }

        
catch (SocketException se)
        
{
            Trace.WriteLine(
"There was an error talking to the server: " +
                se.ToString());
        }

        
finally
        
{
            
// 关闭所有的东西
            if(_client != null
                _client.Close();
        }

    }

}
要使用这个类,你可以简单的创建一个它的实例,然后通过调用ConnectToServer来发送请求。在这个程序中,你首先呼叫了服务端三次,来测试基本的机制。接着,你进入一个循环来真正的“重击”服务端,使得请求数超过服务端默认的线程池容量。这就可以证明服务端在处理多个请求的机制是可靠的。
     static   void  Main( string [] args)
    
{

        MakeClientCallToServer(
"Just wanted to say hi");
        MakeClientCallToServer(
"Just wanted to say hi again");
        MakeClientCallToServer(
"Are you ignoring me?");

        
// 现在发送一堆消息
        string msg;
        
for (int i = 0; i < 100; i++)
        
{
            msg 
= string.Format("I'll not be ignored! (round {0})", i); 
            ThreadPool.QueueUserWorkItem(
new WaitCallback(MakeClientCallToServer), msg);
        }


        Console.WriteLine(
"\n Press any key to continue… (if you can find it…)");
        Console.Read(); 
    }


    
static   void  MakeClientCallToServer( object  objMsg)
    
{
        
string msg = (string)objMsg;
        MyTcpClient client 
= new MyTcpClient(IPAddress.Loopback,55555);
        client.ConnectToServer(msg);
    }
在客户端的输出是这样的:
Sending message to server: Just wanted to say hi
Server said: Thanks call again
!
Sending message to server: Just wanted to say hi again
Server said: Thanks call again
!
Sending message to server: Are you ignoring me
?
Server said: Thanks call again
!
Sending message to server: I
' ll not be ignored! (round 0)
Sending message to server: I ' ll not be ignored! (round 1)
Sending message to server: I ' ll not be ignored! (round 2)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 3)
Sending message to server: I ' ll not be ignored! (round 4)
Sending message to server: I ' ll not be ignored! (round 5)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 6)
Sending message to server: I ' ll not be ignored! (round 7)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 8)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 9)
Sending message to server: I ' ll not be ignored! (round 10)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 11)
Sending message to server: I ' ll not be ignored! (round 12)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 13)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 14)
Sending message to server: I ' ll not be ignored! (round 15)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 16)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 17)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 18)
Sending message to server: I ' ll not be ignored! (round 19)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 20)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 21)
Sending message to server: I ' ll not be ignored! (round 22)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 23)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 24)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 25)
Sending message to server: I ' ll not be ignored! (round 26)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 27)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 28)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 29)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 30)
Sending message to server: I ' ll not be ignored! (round 31)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 32)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 33)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 34)
Sending message to server: I ' ll not be ignored! (round 35)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 36)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 37)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 38)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 39)
Sending message to server: I ' ll not be ignored! (round 40)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 41)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 42)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 43)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 44)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 45)
Sending message to server: I ' ll not be ignored! (round 46)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 47)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 48)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 49)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 50)
Sending message to server: I ' ll not be ignored! (round 51)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 52)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 53)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 54)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 55)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 56)
Sending message to server: I ' ll not be ignored! (round 57)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 58)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 59)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 60)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 61)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 62)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 63)
Sending message to server: I ' ll not be ignored! (round 64)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 65)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 66)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 67)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 68)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 69)
Sending message to server: I ' ll not be ignored! (round 70)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 71)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 72)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 73)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 74)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 75)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 76)
Sending message to server: I ' ll not be ignored! (round 77)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 78)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 79)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 80)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 81)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 82)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 83)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 84)
Sending message to server: I ' ll not be ignored! (round 85)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 86)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 87)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 88)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 89)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 90)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 91)
Sending message to server: I ' ll not be ignored! (round 92)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 93)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 94)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 95)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 96)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 97)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 98)
Server said: Thanks call again !
Sending message to server: I
' ll not be ignored! (round 99)
Server said: Thanks call again !
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
Server said: Thanks call again
!
线程 
0xb10  已退出,返回值为  0  ( 0x0 )。
线程 
' <无名称> '  ( 0x344 ) 已退出,返回值为  0  ( 0x0 )。
线程 
0xe90  已退出,返回值为  0  ( 0x0 )。
线程 
' <无名称> '  ( 0xb64 ) 已退出,返回值为  0  ( 0x0 )。
线程 
0x73c  已退出,返回值为  0  ( 0x0 )。
线程 
0xd9c  已退出,返回值为  0  ( 0x0 )。
线程 
' <无名称> '  ( 0xdf8 ) 已退出,返回值为  0  ( 0x0 )。
线程 
0xe8c  已退出,返回值为  0  ( 0x0 )。
线程 
0x204  已退出,返回值为  0  ( 0x0 )。
线程 
0xc54  已退出,返回值为  0  ( 0x0 )。
线程 
0xad0  已退出,返回值为  0  ( 0x0 )。
线程 
' <无名称> '  ( 0xd34 ) 已退出,返回值为  0  ( 0x0 )。
线程 
0xce8  已退出,返回值为  0  ( 0x0 )。
线程 
0xe88  已退出,返回值为  0  ( 0x0 )。
线程 
0xb6c  已退出,返回值为  0  ( 0x0 )。
线程 
0xaa8  已退出,返回值为  0  ( 0x0 )。
线程 
0xf4  已退出,返回值为  0  ( 0x0 )。
线程 
0xae4  已退出,返回值为  0  ( 0x0 )。
线程 
0x6f8  已退出,返回值为  0  ( 0x0 )。
线程 
' <无名称> '  ( 0x6c4 ) 已退出,返回值为  0  ( 0x0 )。
线程 
0xcfc  已退出,返回值为  0  ( 0x0 )。
线程 
0x504  已退出,返回值为  0  ( 0x0 )。
讨论:
MyTcpClient.ConnectToServer是被设计用来发送一条消息,获得响应,显示响应的内容,然后关闭连接。为了完成这些,它创建一个 System.Net.TcpClient并通过调用 TcpClient.Connect连接到服务端。Connect通过 IPEndPoint定位到服务端,IPEndPoint是根据你传递给MyTcpClient的构造函数的参数(IP地址和端口)确定的。
MyTcpClient.ConnectToServer然后用 Encoding.ASCII.GetBytes 从string中取得bytes。一旦有bytes要发送,它通过调用 System.Net.TcpClient的GetStream方法来获取 NetworkStream,然后通过 TcpClient.Write方法来发送消息。
为了中服务端收到响应,阻塞函数 TcpClient.Read被调用。然后连接被关闭,客户端结束。
写一个TCP服务端

转载于:https://www.cnblogs.com/adaiye/archive/2008/05/11/TcpClient.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值