Socket Class中文解释


Socket 类

.NET Framework 4.6 and 4.5

实现 Berkeley 套接字接口。

System.Object 
  System.Net.Sockets.Socket
命名空间:  System.Net.Sockets
程序集:  System(在 System.dll 中) Socket(SocketInformation) Socket class using the specified value returned from DuplicateAndClose.">使用 DuplicateAndClose 返回的指定的值初始化 Socket 类的新实例。 公共方法 Socket(SocketType, ProtocolType) Socket class using the specified socket type and protocol.">使用指定的地址族、套接字类型和协议初始化 Socket 类的新实例。 公共方法 Socket(AddressFamily, SocketType, ProtocolType) Socket class using the specified address family, socket type and protocol.">使用指定的地址族、套接字类型和协议初始化 Socket 类的新实例。 页首

AddressFamily Socket.">获取 Socket 的地址族。 公共属性 Available 获取已经从网络接收且可供读取的数据量。 公共属性 Blocking Socket is in blocking mode.">获取或设置一个值,该值指示 Socket 是否处于阻止模式。 公共属性 Connected Socket is connected to a remote host as of the last Send or Receive operation.">获取一个值,该值指示 Socket 在上次 Send 或者 Receive 操作时是否连接到远程主机。 公共属性 DontFragment Boolean value that specifies whether the Socket allows Internet Protocol (IP) datagrams to be fragmented.">获取或设置 Boolean 值,该值指定 Socket 是否允许将 Internet 协议 (IP) 数据报分段。 公共属性 DualMode Boolean value that specifies whether the Socket is a dual-mode socket used for both IPv4 and IPv6.">获取或设置一个 Boolean 值,它指定 Socket 是否是用于 IPv4 和 IPv6 的双模式套接字。 公共属性 EnableBroadcast Boolean value that specifies whether the Socket can send or receive broadcast packets.">获取或设置一个 Boolean 值,该值指定 Socket 是否可以发送或接收广播数据包。 公共属性 ExclusiveAddressUse Boolean value that specifies whether the Socket allows only one process to bind to a port.">获取或设置 Boolean 值,该值指定 Socket 是否仅允许一个进程绑定到端口。 公共属性 Handle Socket.">获取 Socket 的操作系统句柄。 公共属性 IsBound Socket is bound to a specific local port.">获取一个值,该值指示 Socket 是否绑定到特定本地端口。 公共属性 LingerState Socket will delay closing a socket in an attempt to send all pending data.">获取或设置一个值,该值指定 Socket 在尝试发送所有挂起数据时是否延迟关闭套接字。 公共属性 LocalEndPoint 获取本地终结点。 公共属性 MulticastLoopback 获取或设置一个值,该值指定传出的多路广播数据包是否传递到发送应用程序。 公共属性 NoDelay Boolean value that specifies whether the stream Socket is using the Nagle algorithm.">获取或设置 Boolean 值,该值指定流 Socket 是否正在使用 Nagle 算法。 公共属性静态成员 OSSupportsIPv4 指示基础操作系统和网络适配器是否支持 Internet 协议第 4 版 (IPv4)。 公共属性静态成员 OSSupportsIPv6 指示基础操作系统和网络适配器是否支持 Internet 协议第 6 版 (IPv6)。 公共属性 ProtocolType Socket.">获取 Socket 的协议类型。 公共属性 ReceiveBufferSize Socket.">获取或设置一个值,它指定 Socket 接收缓冲区的大小。 公共属性 ReceiveTimeout Receive call will time out.">获取或设置一个值,该值指定之后同步 Receive 调用将超时的时间长度。 公共属性 RemoteEndPoint 获取远程终结点。 公共属性 SendBufferSize Socket.">获取或设置一个值,该值指定 Socket 发送缓冲区的大小。 公共属性 SendTimeout Send call will time out.">获取或设置一个值,该值指定之后同步 Send 调用将超时的时间长度。 公共属性 SocketType Socket.">获取 Socket 的类型。 公共属性静态成员 SupportsIPv4 已过时。获取一个值,该值指示在当前主机上 IPv4 支持是否可用并且已启用。 公共属性静态成员 SupportsIPv6 已过时。Dns members.">获取一个值,该值指示 Framework 对某些已过时的Dns 成员是否支持 IPv6。 公共属性 Ttl Socket.">获取或设置一个值,指定 Socket 发送的 Internet 协议 (IP) 数据包的生存时间 (TTL) 值。 公共属性 UseOnlyOverlappedIO 指定套接字是否应仅使用重叠 I/O 模式。 页首

Accept Socket for a newly created connection.">为新建连接创建新的 Socket。 公共方法 AcceptAsync 开始一个异步操作来接受一个传入的连接尝试。 公共方法 BeginAccept(AsyncCallback, Object) 开始一个异步操作来接受一个传入的连接尝试。 公共方法 BeginAccept(Int32, AsyncCallback, Object) 开始异步操作以接受传入的连接尝试并接收客户端应用程序发送的第一个数据块。 公共方法 BeginAccept(Socket, Int32, AsyncCallback, Object) 开始异步操作以接受从指定套接字传入的连接尝试并接收客户端应用程序发送的第一个数据块。 公共方法 BeginConnect(EndPoint, AsyncCallback, Object) 开始一个对远程主机连接的异步请求。 公共方法 BeginConnect(IPAddress, Int32, AsyncCallback, Object) 开始一个对远程主机连接的异步请求。 IPAddress and a port number.">主机由 IPAddress 和端口号指定。 公共方法 BeginConnect(IPAddress[], Int32, AsyncCallback, Object) 开始一个对远程主机连接的异步请求。 IPAddress array and a port number.">主机由 IPAddress 数组和端口号指定。 公共方法 BeginConnect(String, Int32, AsyncCallback, Object) 开始一个对远程主机连接的异步请求。 主机由主机名和端口号指定。 公共方法 BeginDisconnect 开始异步请求从远程终结点断开连接。 公共方法 BeginReceive(IList>>, SocketFlags, AsyncCallback, Object) Socket.">开始从连接的 Socket 中异步接收数据。 公共方法 BeginReceive(IList>>, SocketFlags, SocketError, AsyncCallback, Object) Socket.">开始从连接的 Socket 中异步接收数据。 公共方法 BeginReceive(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) Socket.">开始从连接的 Socket 中异步接收数据。 公共方法 BeginReceive(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object) Socket.">开始从连接的 Socket 中异步接收数据。 公共方法 BeginReceiveFrom 开始从指定网络设备中异步接收数据。 公共方法 BeginReceiveMessageFrom SocketFlags, and stores the endpoint and packet information..">开始使用指定的 SocketFlags 将指定字节数的数据异步接收到数据缓冲区的指定位置,然后存储终结点和数据包信息。 公共方法 BeginSend(IList>>, SocketFlags, AsyncCallback, Object) Socket.">将数据异步发送到连接的 Socket。 公共方法 BeginSend(IList>>, SocketFlags, SocketError, AsyncCallback, Object) Socket.">将数据异步发送到连接的 Socket。 公共方法 BeginSend(Byte[], Int32, Int32, SocketFlags, AsyncCallback, Object) Socket.">将数据异步发送到连接的 Socket。 公共方法 BeginSend(Byte[], Int32, Int32, SocketFlags, SocketError, AsyncCallback, Object) Socket.">将数据异步发送到连接的 Socket。 公共方法 BeginSendFile(String, AsyncCallback, Object) fileName to a connected Socket object using the UseDefaultWorkerThread flag.">使用 UseDefaultWorkerThread 标志,将文件 fileName 发送到连接的 Socket 对象。 公共方法 BeginSendFile(String, Byte[], Byte[], TransmitFileOptions, AsyncCallback, Object) Socket object.">将文件和数据缓冲区异步发送到连接的 Socket 对象。 公共方法 BeginSendTo 向特定远程主机异步发送数据。 公共方法 Bind Socket with a local endpoint.">使 Socket 与一个本地终结点相关联。 公共方法静态成员 CancelConnectAsync 取消一个对远程主机连接的异步请求。 公共方法 Close() Socket connection and releases all associated resources.">关闭 Socket 连接并释放所有关联的资源。 公共方法 Close(Int32) Socket connection and releases all associated resources with a specified timeout to allow queued data to be sent. ">关闭 Socket 连接并释放与指定超时关联的所有资源以允许发送排队数据。 公共方法 Connect(EndPoint) 建立与远程主机的连接。 公共方法 Connect(IPAddress, Int32) 建立与远程主机的连接。 主机由 IP 地址和端口号指定。 公共方法 Connect(IPAddress[], Int32) 建立与远程主机的连接。 主机由 IP 地址的数组和端口号指定。 公共方法 Connect(String, Int32) 建立与远程主机的连接。 主机由主机名和端口号指定。 公共方法 ConnectAsync(SocketAsyncEventArgs) 开始一个对远程主机连接的异步请求。 公共方法静态成员 ConnectAsync(SocketType, ProtocolType, SocketAsyncEventArgs) 开始一个对远程主机连接的异步请求。 公共方法 Disconnect 关闭套接字连接并允许重用套接字。 公共方法 DisconnectAsync 开始异步请求从远程终结点断开连接。 公共方法 Dispose() Socket class.">释放由 Socket 类的当前实例占用的所有资源。 受保护的方法 Dispose(Boolean) Socket, and optionally disposes of the managed resources.">释放由 Socket 使用的非托管资源,并可根据需要释放托管资源。 公共方法 DuplicateAndClose 重复目标进程的套接字引用,并关闭此进程的套接字。 公共方法 EndAccept(IAsyncResult) Socket to handle remote host communication.">异步接受传入的连接尝试,并创建新的 Socket 来处理远程主机通信。 公共方法 EndAccept(Byte[], IAsyncResult) Socket object to handle remote host communication.">异步接受传入的连接尝试,并创建新的 Socket 对象来处理远程主机通信。 此方法返回包含所传输的初始数据的缓冲区。 公共方法 EndAccept(Byte[], Int32, IAsyncResult) Socket object to handle remote host communication.">异步接受传入的连接尝试,并创建新的 Socket 对象来处理远程主机通信。 此方法返回一个缓冲区,其中包含初始数据和传输的字节数。 公共方法 EndConnect 结束挂起的异步连接请求。 公共方法 EndDisconnect 结束挂起的异步断开连接请求。 公共方法 EndReceive(IAsyncResult) 结束挂起的异步读取。 公共方法 EndReceive(IAsyncResult, SocketError) 结束挂起的异步读取。 公共方法 EndReceiveFrom 结束挂起的、从特定终结点进行异步读取。 公共方法 EndReceiveMessageFrom 结束挂起的、从特定终结点进行异步读取。 EndReceiveFrom.">此方法还显示有关数据包而不是 EndReceiveFrom 的更多信息。 公共方法 EndSend(IAsyncResult) 结束挂起的异步发送。 公共方法 EndSend(IAsyncResult, SocketError) 结束挂起的异步发送。 公共方法 EndSendFile 结束文件的挂起异步发送。 公共方法 EndSendTo 结束挂起的、向指定位置进行的异步发送。 公共方法 Equals(Object) 确定指定的对象是否等于当前对象。 (继承自 Object。) 受保护的方法 Finalize Socket class.">释放 Socket 类使用的资源。 (重写 Object.Finalize()。) 公共方法 GetHashCode 作为默认哈希函数。 (继承自 Object。) 公共方法 GetSocketOption(SocketOptionLevel, SocketOptionName) Socket option, represented as an object.">返回指定的 Socket 选项的值,表示为一个对象。 公共方法 GetSocketOption(SocketOptionLevel, SocketOptionName,Byte[]) Socket option setting, represented as a byte array.">返回指定的 Socket 选项设置,表示为字节数组。 公共方法 GetSocketOption(SocketOptionLevel, SocketOptionName, Int32) Socket option in an array.">返回数组中指定的 Socket 选项的值。 公共方法 GetType Type of the current instance.">获取当前实例的 Type。 (继承自 Object。) 公共方法 IOControl(Int32, Byte[], Byte[]) Socket using numerical control codes.">使用数字控制代码,为 Socket 设置低级操作模式。 公共方法 IOControl(IOControlCode, Byte[], Byte[]) Socket using the IOControlCode enumeration to specify control codes.">使用 IOControlCode 枚举指定控制代码,为 Socket 设置低级操作模式。 公共方法 Listen Socket in a listening state.">将 Socket 置于侦听状态。 受保护的方法 MemberwiseClone Object.">创建当前 Object 的浅表副本。 (继承自 Object。) 公共方法 Poll Socket.">确定 Socket 的状态。 公共方法 Receive(IList>>) Socket into the list of receive buffers.">从绑定的 Socket 接收数据,将数据存入接收缓冲区列表中。 公共方法 Receive(Byte[]) Socket into a receive buffer.">从绑定的 Socket 套接字接收数据,将数据存入接收缓冲区。 公共方法 Receive(IList>>, SocketFlags) Socket into the list of receive buffers, using the specified SocketFlags.">使用指定的 SocketFlags,从绑定的 Socket 接收数据,将数据存入接收缓冲区列表中。 公共方法 Receive(Byte[], SocketFlags) Socket into a receive buffer, using the specified SocketFlags.">使用指定的 SocketFlags,从绑定的 Socket 接收数据,将数据存入接收缓冲区。 公共方法 Receive(IList>>, SocketFlags, SocketError) Socket into the list of receive buffers, using the specified SocketFlags.">使用指定的 SocketFlags,从绑定的 Socket 接收数据,将数据存入接收缓冲区列表中。 公共方法 Receive(Byte[], Int32, SocketFlags) Socket into a receive buffer, using the specified SocketFlags.">使用指定的 SocketFlags,从绑定的 Socket 接收指定字节数的数据,并将数据存入接收缓冲区。 公共方法 Receive(Byte[], Int32, Int32, SocketFlags) Socket into the specified offset position of the receive buffer, using the specified SocketFlags.">使用指定的 SocketFlags,从绑定的 Socket 接收指定的字节数,存入接收缓冲区的指定偏移量位置。 公共方法 Receive(Byte[], Int32, Int32, SocketFlags, SocketError) Socket into a receive buffer, using the specified SocketFlags.">使用指定的 SocketFlags,从绑定的 Socket 接收数据,将数据存入接收缓冲区。 公共方法 ReceiveAsync Socket object.">开始一个异步请求以便从连接的 Socket 对象中接收数据。 公共方法 ReceiveFrom(Byte[], EndPoint) 将数据报接收到数据缓冲区并存储终结点。 公共方法 ReceiveFrom(Byte[], SocketFlags, EndPoint) SocketFlags, and stores the endpoint.">使用指定的 SocketFlags 将数据报接收到数据缓冲区并存储终结点。 公共方法 ReceiveFrom(Byte[], Int32, SocketFlags, EndPoint) SocketFlags, and stores the endpoint.">使用指定的 SocketFlags 将指定的字节数接收到数据缓冲区并存储终结点。 公共方法 ReceiveFrom(Byte[], Int32, Int32, SocketFlags, EndPoint) SocketFlags, and stores the endpoint.">使用指定的 SocketFlags 将指定字节数的数据接收到数据缓冲区的指定位置并存储终结点。 公共方法 ReceiveFromAsync 开始从指定网络设备中异步接收数据。 公共方法 ReceiveMessageFrom SocketFlags, and stores the endpoint and packet information.">使用指定的 SocketFlags 将指定字节数的数据接收到数据缓冲区的指定位置,然后存储终结点和数据包信息。 公共方法 ReceiveMessageFromAsync SocketAsyncEventArgs.SocketFlags, and stores the endpoint and packet information.">开始使用指定的 SocketAsyncEventArgs.SocketFlags 将指定字节数的数据异步接收到数据缓冲区的指定位置,并存储终结点和数据包信息。 公共方法静态成员 Select 确定一个或多个套接字的状态。 公共方法 Send(IList>>) Socket.">将列表中的一组缓冲区发送到连接的 Socket。 公共方法 Send(Byte[]) Socket.">将数据发送到连接的 Socket。 公共方法 Send(IList>>, SocketFlags) Socket, using the specified SocketFlags.">使用指定的 SocketFlags,将列表中的一组缓冲区发送到连接的Socket。 公共方法 Send(Byte[], SocketFlags) Socket using the specified SocketFlags.">使用指定的 SocketFlags 将数据发送到连接的 Socket。 公共方法 Send(IList>>, SocketFlags, SocketError) Socket, using the specified SocketFlags.">使用指定的 SocketFlags,将列表中的一组缓冲区发送到连接的Socket。 公共方法 Send(Byte[], Int32, SocketFlags) Socket, using the specified SocketFlags.">使用指定的 SocketFlags,将指定字节数的数据发送到已连接的Socket。 公共方法 Send(Byte[], Int32, Int32, SocketFlags) Socket, starting at the specified offset, and using the specified SocketFlags.">使用指定的 SocketFlags,将指定字节数的数据发送到已连接的Socket(从指定的偏移量开始)。 公共方法 Send(Byte[], Int32, Int32, SocketFlags, SocketError) Socket, starting at the specified offset, and using the specified SocketFlags">从指定的偏移量开始使用指定的 SocketFlags 将指定字节数的数据发送到连接的 Socket。 公共方法 SendAsync Socket object.">将数据异步发送到连接的 Socket 对象。 公共方法 SendFile(String) fileName to a connected Socket object with the UseDefaultWorkerThread transmit flag.">使用 UseDefaultWorkerThread 传输标志,将文件 fileName发送到连接的 Socket 对象。 公共方法 SendFile(String, Byte[], Byte[], TransmitFileOptions) fileName and buffers of data to a connected Socket object using the specified TransmitFileOptions value.">使用指定的 TransmitFileOptions 值,将文件 fileName 和数据缓冲区发送到连接的 Socket 对象。 公共方法 SendPacketsAsync Socket object.">将文件集合或者内存中的数据缓冲区以异步方法发送给连接的Socket 对象。 公共方法 SendTo(Byte[], EndPoint) 将数据发送到指定的终结点。 公共方法 SendTo(Byte[], SocketFlags, EndPoint) SocketFlags.">使用指定的 SocketFlags,将数据发送到特定的终结点。 公共方法 SendTo(Byte[], Int32, SocketFlags, EndPoint) SocketFlags.">使用指定的 SocketFlags,将指定字节数的数据发送到指定的终结点。 公共方法 SendTo(Byte[], Int32, Int32, SocketFlags, EndPoint) SocketFlags.">使用指定的 SocketFlags,将指定字节数的数据发送到指定终结点(从缓冲区中的指定位置开始)。 公共方法 SendToAsync 向特定远程主机异步发送数据。 公共方法 SetIPProtectionLevel 设置套接字的 IP 保护级别。 公共方法 SetSocketOption(SocketOptionLevel, SocketOptionName, Boolean) Socket option to the specified Boolean value.">将指定的 Socket 选项设置为指定的 Boolean 值。 公共方法 SetSocketOption(SocketOptionLevel, SocketOptionName,Byte[]) Socket option to the specified value, represented as a byte array.">将指定的 Socket 选项设置为指定的值,表示为字节数组。 公共方法 SetSocketOption(SocketOptionLevel, SocketOptionName, Int32) Socket option to the specified integer value.">将指定的 Socket 选项设置为指定的整数值。 公共方法 SetSocketOption(SocketOptionLevel, SocketOptionName, Object) Socket option to the specified value, represented as an object.">将指定的 Socket 选项设置为指定值,表示为对象。 公共方法 Shutdown Socket.">禁用某 Socket 上的发送和接收。 公共方法 ToString 返回表示当前对象的字符串。 (继承自 Object。) 页首

ProtocolType 枚举中所列出的任何一种协议执行异步和同步数据传输。

Socket class follows the .NET Framework naming pattern for asynchronous methods.">Socket 类对异步方法遵循 .NET Framework 命名模式。 Receive method corresponds to the asynchronous BeginReceive and EndReceive methods.">例如,同步的 Receive 方法对应于异步的 BeginReceive 和 EndReceive 方法。

如果应用程序在执行期间只需要一个线程,请使用下面的方法,这些方法适用于同步操作模式。

  • Listen method.">如果当前使用的是面向连接的协议(如 TCP),则服务器可以使用 Listen 方法侦听连接。 Accept method processes any incoming connection requests and returns a Socket that you can use to communicate data with the remote host.">Accept 方法处理任何传入的连接请求,并返回可用于与远程主机进行数据通信的 Socket。 Socket to call the Send or Receive method.">可以使用此返回的 Socket 来调用 Send 或 Receive 方法。 Bind method prior to calling the Listen method if you want to specify the local IP address and port number.">如果要指定本地 IP 地址和端口号,请在调用 Listen 方法之前先调用 Bind 方法。 如果您希望基础服务提供程序为您分配可用端口,请使用端口号 0。 Connect method.">如果希望连接到侦听主机,请调用 Connect 方法。 Send or Receive method.">若要进行数据通信,请调用 Send 或 Receive 方法。

  • 如果当前使用的是无连接协议(如 UDP),则根本不需要侦听连接。 ReceiveFrom method to accept any incoming datagrams.">调用 ReceiveFrom 方法可接受任何传入的数据报。 SendTo method to send datagrams to a remote host.">使用 SendTo 方法可将数据报发送到远程主机。

若要在执行过程中使用单独的线程处理通信,请使用下面的方法,这些方法适用于异步操作模式。

  • Socket, BeginConnect, and EndConnect methods to connect with a listening host.">如果当前使用的是面向连接的协议(如 TCP),则可使用 Socket、BeginConnect 和 EndConnect 方法来连接侦听主机。 BeginSend and EndSend or BeginReceive and EndReceive methods to communicate data asynchronously.">通过使用 BeginSend 和 EndSend 方法,或者使用 BeginReceive 和EndReceive 方法,可以进行异步数据通信。 BeginAccept and EndAccept.">可以使用 BeginAccept 和 EndAccept 处理传入的连接请求。

  • BeginSendTo and EndSendTo to send datagrams, and BeginReceiveFrom and EndReceiveFrom to receive datagrams.">如果您使用的是 UDP 等无连接协议,则可以使用 BeginSendTo 和 EndSendTo 来发送数据报,而使用 BeginReceiveFrom 和 EndReceiveFrom 来接收数据报。

如果对一个套接字执行多个异步操作,它们不一定按启动时的顺序完成。

Shutdown method to disable the Socket.">当数据发送和数据接收完成之后,可使用 Shutdown 方法来禁用 Socket。 Shutdown, call the Close method to release all resources associated with the Socket.">在调用 Shutdown 之后,可调用 Close 方法来释放与 Socket 关联的所有资源。

Socket class allows you to configure your Socket using the SetSocketOption method.">通过 Socket 类,您可以使用 SetSocketOption 方法来配置 Socket。 GetSocketOption method.">可以使用 GetSocketOption 方法来检索这些设置。

说明说明

TcpClient, TcpListener, and UdpClient.">如果要编写相对简单的应用程序,而且不需要最高的性能,则可以考虑使用 TcpClient、TcpListener 和 UdpClient。 Socket communications.">这些类为 Socket 通信提供了更简单、对用户更友好的接口。

示例

 

Socket class can be used to send data to an HTTP server and receive the response.">下面的代码示例演示如何使用 Socket 类向 HTTP 服务器发送数据和接收响应。 接收到整个页前,此示例将一直处于阻止状态。

C#
using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;

public class GetSocket
{
    private static Socket ConnectSocket(string server, int port)
    {
        Socket s = null;
        IPHostEntry hostEntry = null;

        // Get host related information.
        hostEntry = Dns.GetHostEntry(server);

        // Loop through the AddressList to obtain the supported AddressFamily. This is to avoid
        // an exception that occurs when the host IP Address is not compatible with the address family
        // (typical in the IPv6 case).
        foreach(IPAddress address in hostEntry.AddressList)
        {
            IPEndPoint ipe = new IPEndPoint(address, port);
            Socket tempSocket = 
                new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp);

            tempSocket.Connect(ipe);

            if(tempSocket.Connected)
            {
                s = tempSocket;
                break;
            }
            else
            {
                continue;
            }
        }
        return s;
    }

    // This method requests the home page content for the specified server.
    private static string SocketSendReceive(string server, int port) 
    {
        string request = "GET / HTTP/1.1\r\nHost: " + server + 
            "\r\nConnection: Close\r\n\r\n";
        Byte[] bytesSent = Encoding.ASCII.GetBytes(request);
        Byte[] bytesReceived = new Byte[256];

        // Create a socket connection with the specified server and port.
        Socket s = ConnectSocket(server, port);

        if (s == null)
            return ("Connection failed");

        // Send request to the server.
        s.Send(bytesSent, bytesSent.Length, 0);  

        // Receive the server home page content.
        int bytes = 0;
        string page = "Default HTML page on " + server + ":\r\n";

        // The following will block until te page is transmitted.
        do {
            bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
            page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
        }
        while (bytes > 0);

        return page;
    }

    public static void Main(string[] args) 
    {
        string host;
        int port = 80;

        if (args.Length == 0)
            // If no server name is passed as argument to this program, 
            // use the current host name as the default.
            host = Dns.GetHostName();
        else
            host = args[0];

        string result = SocketSendReceive(host, port); 
        Console.WriteLine(result);
    }
}
  线程安全  

此类的实例是线程安全的。