.NET Framework 4.6 and 4.5
实现 Berkeley 套接字接口。
命名空间: System.Net.Sockets
程序集: System(在 System.dll 中)
Socket 类型公开以下成员。
显示:
| 名称 | 说明 | |
|---|---|---|
| Socket(SocketInformation) | Socket 类的新实例。 | |
| Socket(SocketType, ProtocolType) | Socket 类的新实例。 | |
| Socket(AddressFamily, SocketType, ProtocolType) | Socket 类的新实例。 |
显示:
| 名称 | 说明 | |
|---|---|---|
| AddressFamily | Socket 的地址族。 | |
| Available | 获取已经从网络接收且可供读取的数据量。 | |
| Blocking | Socket 是否处于阻止模式。 | |
| Connected | Receive 操作时是否连接到远程主机。 | |
| DontFragment | Socket 是否允许将 Internet 协议 (IP) 数据报分段。 | |
| DualMode | Socket 是否是用于 IPv4 和 IPv6 的双模式套接字。 | |
| EnableBroadcast | Socket 是否可以发送或接收广播数据包。 | |
| ExclusiveAddressUse | Socket 是否仅允许一个进程绑定到端口。 | |
| Handle | Socket 的操作系统句柄。 | |
| IsBound | Socket 是否绑定到特定本地端口。 | |
| LingerState | Socket 在尝试发送所有挂起数据时是否延迟关闭套接字。 | |
| LocalEndPoint | 获取本地终结点。 | |
| MulticastLoopback | 获取或设置一个值,该值指定传出的多路广播数据包是否传递到发送应用程序。 | |
| NoDelay | Socket 是否正在使用 Nagle 算法。 | |
|
|
OSSupportsIPv4 | 指示基础操作系统和网络适配器是否支持 Internet 协议第 4 版 (IPv4)。 |
|
|
OSSupportsIPv6 | 指示基础操作系统和网络适配器是否支持 Internet 协议第 6 版 (IPv6)。 |
| ProtocolType | Socket 的协议类型。 | |
| ReceiveBufferSize | Socket 接收缓冲区的大小。 | |
| ReceiveTimeout | Receive 调用将超时的时间长度。 | |
| RemoteEndPoint | 获取远程终结点。 | |
| SendBufferSize | Socket 发送缓冲区的大小。 | |
| SendTimeout | Send 调用将超时的时间长度。 | |
| SocketType | Socket 的类型。 | |
|
|
SupportsIPv4 | 已过时。获取一个值,该值指示在当前主机上 IPv4 支持是否可用并且已启用。 |
|
|
SupportsIPv6 | 已过时。Dns 成员是否支持 IPv6。 |
| Ttl | Socket 发送的 Internet 协议 (IP) 数据包的生存时间 (TTL) 值。 | |
| UseOnlyOverlappedIO | 指定套接字是否应仅使用重叠 I/O 模式。 |
显示:
ProtocolType 枚举中所列出的任何一种协议执行异步和同步数据传输。
EndReceive 方法。
如果应用程序在执行期间只需要一个线程,请使用下面的方法,这些方法适用于同步操作模式。
-
Receive 方法。
-
SendTo 方法可将数据报发送到远程主机。
若要在执行过程中使用单独的线程处理通信,请使用下面的方法,这些方法适用于异步操作模式。
-
EndAccept 处理传入的连接请求。
-
EndReceiveFrom 来接收数据报。
如果对一个套接字执行多个异步操作,它们不一定按启动时的顺序完成。
Socket 关联的所有资源。
GetSocketOption 方法来检索这些设置。
|
|
|---|
|
Socket 通信提供了更简单、对用户更友好的接口。 |
接收到整个页前,此示例将一直处于阻止状态。
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); } }