C# 之TcpClient和Socket

OSI七层网络架构

OSI中的层 功能 TCP/IP协议族
应用层 文件传输,电子邮件,文件服务,虚拟终端; TFTP,HTTP,SNMP,FTP,SMTP,DNS,Telnet
表示层 数据格式化,代码转换,数据加密; 没有协议
会话层 解除或建立与别的接点的联系; 没有协议
传输层 提供端对端的接口; TCP,UDP
网络层 为数据包选择路由; IP,ICMP,RIP,OSPF,BGP,IGMP
数据链路层 传输有地址的帧以及错误检测功能; SLIP,CSLIP,PPP,ARP,RARP,MTU
物理层 以二进制数据形式在物理媒体上传输数据; ISO2110,IEEE802,IEEE802.2


简单的理解

 物理层:HUB,网线   
 链路层:MAC,ARP,交换机   
 网络层:IP,ICMP,IGMP,路由器   
 传输层:TCP,UDP   
 会话层:HTTP,SMTP,FTP,POP3   
 表示层:SOAP,SSL   
 应用层:WebService的Method 
-------------------------------  
  Socket是对网络层操作  
  TcpClient是对传输层操作  
  ASP.NET是对会话层操作  
-------------------------------
TcpClient是Socket的基础上的封装,为了简化一部分Socket的功能。
1>Socket支持TCP,UDP,IP包,Stream,Dgram等诸多类型
2>而TcpClient只支持TCP和Stream
一般的应用,用TcpClient可以了,或者使用NetStream,如果要做点高级的事情,建议用Socket做。

------------------------------

TcpClient 类(为 TCP 网络服务提供客户端连接) 

备注

TcpClient 类提供了一些简单的方法,用于在同步阻止模式下通过网络来连接、发送和接收流数据。
为使 TcpClient 连接并交换数据,使用 TCP ProtocolType 创建的 TcpListener 或 Socket 必须侦听是否有传入的连接请求。可以使用下面两种方法之一连接到该侦听器:
创建一个 TcpClient,并调用三个可用的 Connect 方法之一。
使用远程主机的主机名和端口号创建 TcpClient。此构造函数将自动尝试一个连接。

Note注意

如果要在同步阻止模式下发送无连接数据报,请使用 UdpClient 类。
继承说明 要发送和接收数据,请使用 GetStream 方法来获取一个 NetworkStream。调用 NetworkStream 的 Write 和 Read 方法与远程主机之间发送和接收数据。使用 Close 方法释放与 TcpClient 关联的所有资源。

下面的代码示例建立 TcpClient 连接

[csharp]  view plain  copy
  1. static void Connect(String server, String message)   
  2. {  
  3.   try   
  4.   {  
  5.     // Create a TcpClient.  
  6.     // Note, for this client to work you need to have a TcpServer   
  7.     // connected to the same address as specified by the server, port  
  8.     // combination.  
  9.     Int32 port = 13000;  
  10.     TcpClient client = new TcpClient(server, port);  
  11.       
  12.     // Translate the passed message into ASCII and store it as a Byte array.  
  13.     Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);           
  14.   
  15.     // Get a client stream for reading and writing.  
  16.    //  Stream stream = client.GetStream();  
  17.       
  18.     NetworkStream stream = client.GetStream();  
  19.   
  20.     // Send the message to the connected TcpServer.   
  21.     stream.Write(data, 0, data.Length);  
  22.   
  23.     Console.WriteLine("Sent: {0}", message);           
  24.   
  25.     // Receive the TcpServer.response.  
  26.       
  27.     // Buffer to store the response bytes.  
  28.     data = new Byte[256];  
  29.   
  30.     // String to store the response ASCII representation.  
  31.     String responseData = String.Empty;  
  32.   
  33.     // Read the first batch of the TcpServer response bytes.  
  34.     Int32 bytes = stream.Read(data, 0, data.Length);  
  35.     responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);  
  36.     Console.WriteLine("Received: {0}", responseData);           
  37.   
  38.     // Close everything.  
  39.     stream.Close();           
  40.     client.Close();           
  41.   }   
  42.   catch (ArgumentNullException e)   
  43.   {  
  44.     Console.WriteLine("ArgumentNullException: {0}", e);  
  45.   }   
  46.   catch (SocketException e)   
  47.   {  
  48.     Console.WriteLine("SocketException: {0}", e);  
  49.   }  
  50.       
  51.   Console.WriteLine("\n Press Enter to continue...");  
  52.   Console.Read();  
  53. }  

猜你喜欢

转载自blog.csdn.net/lei_7103/article/details/76067230
今日推荐