C# Socket编程(4)初识Socket和数据流

[来源] 达内    [编辑] 达内   [时间]2012-10-16

经过前面基础知识作为背景,现在对Socket编程进行进一步的学习

经过前面基础知识作为背景,现在对Socket编程进行进一步的学习。在  System.Net.Socket  命名空间提供了 Socket 类,利用该类我们可以直接编写Socket的客户端和服务的的程序。但是直接使用Socket类编写Socket程序会比较麻烦、而且容易出错,所以.NET为我们提供了进一步封装好的TcpListener类、 TCPClient 类和UdpClient 类。同时,当我们希望通过网络传输数据时,首先应该将数据转换为数据流。

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 阅读目录:

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   1.Socket的类型        

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   2.第一个Socket程序

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">       2.1 服务端程序

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">       2.2 客户端程序

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">       2.3 程序运行效果

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   3.网络流和内存流

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">       3.1 网络流

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">       3.2 内存流

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   4.进一步阅读和参考资料

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   5.示例下载

< h3 style="font-size: 16px; font-weight: bold; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0px; color: white; background-color: rgb(36, 162, 162); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: bot h; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">1.Socket的类型 < p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   Socket的中文释义称为套接字,是支撑TCP/IP通信最基本的操作单元。可以将Socket看做不同主机之间的进程进行双向通信的端点,在一个双方都可以通信的Socket实例中,既保存了对方的IP地址和端口,也保持了双方通信采用的协议等信息。Socket有三种不同的类型:

< blockquote style="background-image: url(http://images.cnblogs.com/cnblogs_com/IPrograming/353645/o_o_yellow-pin.png); background-color: rgb(255, 255, 163); border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); margin: 10px 0px; padding: 15px 20px 15px 60px; font-size: 15px; font-family: 'Mic rosoft Yahei'; box-shadow: rgb(170, 170, 170) 0px 0px 8px; clear: both; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 9px 0px; background-repeat: no-repeat no-repeat; ">
  • ①.流套接字:实现面向连接的TCP通信
  • ②.数据报套接字:实现无连接的UDP通信
  • ③.原始套接字:实现IP数据包的通信(这里不做讨论)
< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 三种类型的套接字的对象均可使用Socket类来构造:

< div style="background-color: rgb(229, 229, 204); font-family: 'Courier New'; font-size: 14px; border: 0px solid rgb(147, 147, 147); padding: 10px 20px; overflow: auto; margin: 5px 0px; clear: both; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: no rmal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; " class="cnblogs_code">
1
         /// <summary>

2         ///
 Socket 构造函数 3
         ///

 </summary>

4         ///
 <param name="addressFamily">网络类型
</param>

5         ///
 <param name="socketType">Socket类型
</param>

6         ///
 <param name="protocolType">Socket使用的协议
</param>

7         public
 Socket(AddressFamily addressFamily,SocketType socketType,ProtocolType protocolType)
< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">当我们编写基于TCP和UDP的应用程序时,既可以使用对套接字进行进一步封装的TcpListener 类、TCPClient类和UdpClient类,也可以直接使用Socket类来实现,如果没有特殊需求应该使用进一步封装过的类,由于Socket类是他们实现的基础 ,所有在这里我们先从学习Socket类入手。

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< h3 style="font-size: 16px; font-weight: bold; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0px; color: white; background-color: rgb(36, 162, 162); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: bot h; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">2.第一个Socket程序 < p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   在 C# Socket编程(1)基本的术语和概念 这篇博客中我们知道:IP协议层之上是传输层(transport layer),它提供了两种可选的协议:TCP协议和UDP协议,它们分别是面向连接无连接 的两种协议。在面向连接的Socket中,使用TCP来建立两个地址端点的会话。一旦建立这种连接,就可以在设备之间进行可靠的数据传输。在进行跟深入的学习前我们先巩固一个简单的例子(TCP)来对Socket编程建立一个直观的印象。  

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">  TCP Socket连接的过程可以简单的分为: ①.服务端监听 ②.客户端请求 ③.建立连接 在使用面向连接的Socket进行通信之前,两个应用程序之间首先要建立一个TCP连接,这涉及两台相互通信的主机的TCP部件间完成的握手消息(handshake message)的交换。下面我们通过直接使用 Socket 类来构建一个简单的Socket应用程序(这里先从同步Socket入手,实际项目要比这复杂,有许多需要考虑的问题:如消息边界问题、端口号是否冲突、消息命令的解析等等)。在这里我们为了和每一个客户端进行通信建立两个线程:一个是接受客户端连接的线程,一个是接受客户端数据的线程,下面是分别是示例程序的服务端和客户端的代码:

< h4 style="margin: 10px 0px; padding: 2px 10px; font-family: 'Microsoft Yahei'; font-size: 14px; color: white; background-color: rgb(57, 122, 173); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: both; font-style: norm al; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">2.1 服务器端代码: < div style="background-color: rgb(229, 229, 204); font-family: 'Courier New'; font-size: 14px; border: 0px solid rgb(147, 147, 147); padding: 10px 20px; overflow: auto; margin: 5px 0px; clear: both; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: no rmal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; " class="cnblogs_code"> View Code
< h4 style="margin: 10px 0px; padding: 2px 10px; font-family: 'Microsoft Yahei'; font-size: 14px; color: white; background-color: rgb(57, 122, 173); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: both; font-style: norm al; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; "> 2.2 客户端代码:  < div style="background-color: rgb(229, 229, 204); font-family: 'Courier New'; font-size: 14px; border: 0px solid rgb(147, 147, 147); padding: 10px 20px; overflow: auto; margin: 5px 0px; clear: both; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: no rmal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; " class="cnblogs_code"> View Code
< h4 style="margin: 10px 0px; padding: 2px 10px; font-family: 'Microsoft Yahei'; font-size: 14px; color: white; background-color: rgb(57, 122, 173); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: both; font-style: norm al; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">2.3 运行示例程序 < p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 首先运行服务端程序:

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">  

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">接着运行客户端程序,向服务端发送消息后

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">  

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">这时候我们可以看到服务端已经收到了客户端发送的消息

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">  

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< h3 style="font-size: 16px; font-weight: bold; padding: 6px 20px; font-family: 'Microsoft Yahei'; margin: 15px 0px; color: white; background-color: rgb(36, 162, 162); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: bot h; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">3.网络流和内存流 < p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">  通过网络传输数据,或者对文件数据进行操作的时候都需要先将数据转换为数据流 。典型的数据流是 和某个外部数据源相关,数据源可以是文件、外部设备、内存、网络套接字等。.NET提供多个从Stream类派生的子类来对不同的数据源提供支持,每个类都代表了一种具体的数据流类型。例如和磁盘文件相关的文件流FileStream和Socket 关的NetworkStream,和内存相关的MemoryStream等,在Socket编程中我们只需了解NetworkStream和MemoryStream(具体文件IO可以参考博文:.NET I/O 学习笔记:文件的读和写),一个用来网络数据的传输,另一个用作数据缓冲区。

< h4 style="margin: 10px 0px; padding: 2px 10px; font-family: 'Microsoft Yahei'; font-size: 14px; color: white; background-color: rgb(57, 122, 173); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: both; font-style: norm al; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">3.1 网络流(NetworkStream) < p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   数据在网络的各个位置之间是以连续的字节形式传输的,我们使用 NetworkStream 类来发送和接收网络数据。和其他的的流类型不同NetworkStream 类是在System.Net.Sockets  命名空间中的,该类实现专门用于网络资源的 Stream  类。 NetworkStream 选件类和其他流之间的主要差异在于NetworkStream 没有当前位置的概念,因此不支持查找功能,并且NetworkStream仅支持面向连接(TCP)的Socket

< blockquote style="background-image: url(http://images.cnblogs.com/cnblogs_com/IPrograming/353645/o_o_yellow-pin.png); background-color: rgb(255, 255, 163); border: 2px solid rgb(239, 239, 239); color: rgb(51, 51, 51); margin: 10px 0px; padding: 15px 20px 15px 60px; font-size: 15px; font-family: 'Mic rosoft Yahei'; box-shadow: rgb(170, 170, 170) 0px 0px 8px; clear: both; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: 9px 0px; background-repeat: no-repeat no-repeat; ">

  对于NetworkStream来说,写入操作是指将数据源内存缓冲区到网络上的数据传输;读取操作是从网络上到接收端内存缓冲区的数据传输。

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">创建NetworkStream对象

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">  我们可以通过 TcpClient对象的GetStream() 方法获取该对象发送和接收数据的 NetworkStream 对象:

< div style="background-color: rgb(229, 229, 204); font-family: 'Courier New'; font-size: 14px; border: 0px solid rgb(147, 147, 147); padding: 10px 20px; overflow: auto; margin: 5px 0px; clear: both; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: no rmal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; " class="cnblogs_code">
1
             TcpClient client = new TcpClient(); 
2             client.Connect("
www.baidu.com

", 8099
); 3             NetworkStream nStream = client.GetStream();
< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">    也可以通过使用Socket来获取 NetworkStream 对象:

< div style="background-color: rgb(229, 229, 204); font-family: 'Courier New'; font-size: 14px; border: 0px solid rgb(147, 147, 147); padding: 10px 20px; overflow: auto; margin: 5px 0px; clear: both; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: no rmal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; " class="cnblogs_code">
1
 NetworkStream myNetworkStream = new NetworkStream(mySocket);     
< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">通过NetworkStream对象获取数据

< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">   接收数据端通过调用Read方法将数据从接收缓冲区中读取到进程缓冲区中,完成读取操作。可以通过调用DataAvailable 属性来确定是否还有数据可供读取,如下:

< div style="background-color: rgb(229, 229, 204); font-family: 'Courier New'; font-size: 14px; border: 0px solid rgb(147, 147, 147); padding: 10px 20px; overflow: auto; margin: 5px 0px; clear: both; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: no rmal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; " class="cnblogs_code">
复制代码
 1
            TcpClient client = new TcpClient(); 
 2             client.Connect("
www.baidu.com

", 8099
);  3
             NetworkStream nStream =

 client.GetStream();  4             //
是否有数据可读

 5             if
 (nStream.CanRead)  6
             { 

 7                 //
接受数据的缓冲区

 8                 byte
[] myReadBuffer = new byte
[1024

];  9
                 StringBuilder completeMessage = new StringBuilder(); 
10                 int
 numberOfBytesRead = 0

; 11
                 //准备接收的信息也有可能大于1024所以使用循环

12                 do
13                 { 
14                     numberOfBytesRead = nStream.Read(myReadBuffer,0
,myReadBuffer.Length); 15
                     completeMessage.AppendFormat("{0}
"

,Encoding.UTF8.GetString(myReadBuffer,0,numberOfBytesRead); 
16                 }while


(nStream.DataAvailable); 17                 Console.WriteLine("
接受的信息为:"
+completeMessage); 18
             } 

19             else

20             { 
21                 Console.WriteLine("


当前没有可供读取的数据。"); 
22             }
复制代码
< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

< h4 style="margin: 10px 0px; padding: 2px 10px; font-family: 'Microsoft Yahei'; font-size: 14px; color: white; background-color: rgb(57, 122, 173); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; clear: both; font-style: norm al; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; ">3.2 内存流(MemoryStream) < p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); ">    MemoryStream 表示保存在内存中的数据流,有该类封装的数据可以直接在内存中访问。内存流一般用于暂时缓 存数据已降低应用程序对临时缓冲区和临时文件的需要。内存流相对于字节数组容量可以自动增长,并且在需要对数据进行加密以及对数据长度不定的数据进行缓存时,使用内存流比较方便。MemoryStream支持对数据流的查找和随机访问,当该类对象的 CanSeek属性值为True时,程序可以通过范围 Position属性获取内存流当前的位置。下面我们通过一个简单的小示例学习如何具体使用内存流:

< div style="background-color: rgb(229, 229, 204); font-family: 'Courier New'; font-size: 14px; border: 0px solid rgb(147, 147, 147); padding: 10px 20px; overflow: auto; margin: 5px 0px; clear: both; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: no rmal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-position: initial initial; background-repeat: initial initial; " class="cnblogs_code">
复制代码
 1
         static void
 Main(

string[] args)  2
         {  3
             //

构造MemoryStream实例

 4             MemoryStream m_Stream = new
 MemoryStream();  5
             Console.WriteLine("初始化分配容量:{0}
"

, m_Stream.Capacity);  6
             Console.WriteLine("初始使用量:{0}
", m_Stream.Length); 
 7

 
 8             //
将待写入数据从字符串转换为字节数组

 9             UnicodeEncoding encoder = new
 UnicodeEncoding(); 10
             byte[] bytes = encoder.GetBytes("
新增数据

"); 
11
 
12             //
向内存流中写入数据

13             for
 (int i = 0
; i < 4

; i++) 14
             { 15
                 Console.WriteLine("

第{0}写入新数据"
, i); 16
                 m_Stream.Write(bytes, 0, bytes.Length); 
17

             } 18
 
19             //
写入数据后MemoryStream实例的容量和使用量的大小

20             Console.WriteLine("
当前分配容量:{0}"
, m_Stream.Capacity); 21
             Console.WriteLine("

当前使用量:{0}"
, m_Stream.Length); 22
 
23             Console.Read(); 
24         }
复制代码
< p style="margin: 7px 0px; text-indent: 0px; line-height: 24px; color: rgb(0, 0, 0); font-family: 微软雅黑, 宋体, Arial; font-size: 15px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: -webkit-auto; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); "> 

资源下载