| 名称 | 说明 | |
|---|---|---|
![]() |
Socket(AddressFamily, SocketType, ProtocolType) |
新实例初始化 Socket 类使用指定的地址族、 套接字类型和协议。 |
![]() |
Socket(SocketInformation) |
新实例初始化 Socket 类使用返回值的指定的值 DuplicateAndClose。 |
![]() |
Socket(SocketType, ProtocolType) |
新实例初始化 Socket 类使用指定的套接字类型和协议。 |
| 名称 | 说明 | |
|---|---|---|
![]() |
AddressFamily |
获取的地址族 Socket。 |
![]() |
Available |
获取已经从网络接收且可供读取的数据量。 |
![]() |
Blocking |
获取或设置一个值,该值指示是否 Socket 处于阻塞模式。 |
![]() |
Connected | |
![]() |
DontFragment |
获取或设置 Boolean 值,该值指定是否 Socket 允许 Internet 协议 (IP) 的数据报进行分片。 |
![]() |
DualMode |
获取或设置一个 Boolean 值,该值指定 Socket 是否是用于 IPv4 和 IPv6 的双模式套接字。 |
![]() |
EnableBroadcast |
获取或设置 Boolean 值,该值指定是否 Socket 可以发送或接收广播的数据包。 |
![]() |
ExclusiveAddressUse |
获取或设置 Boolean 值,该值指定是否 Socket 仅允许一个进程绑定到端口。 |
![]() |
Handle |
获取操作系统句柄 Socket。 |
![]() |
IsBound |
获取一个值,该值指示是否 Socket 绑定到特定本地端口。 |
![]() |
LingerState |
获取或设置一个值,指定是否 Socket 将延迟发送所有挂起的数据以尝试关闭套接字。 |
![]() |
LocalEndPoint |
获取本地终结点。 |
![]() |
MulticastLoopback |
获取或设置一个值,指定是否传出多播的数据包将传输到发送应用程序。 |
![]() |
NoDelay |
获取或设置 Boolean 值,该值指定是否流 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 |
已过时。 获取一个值,该值指示框架是否支持 IPv6 对于某些已过时 Dns 成员。 |
![]() |
Ttl |
获取或设置一个值,指定发送的 Internet 协议 (IP) 数据包的生存时间 (TTL) 值 Socket。 |
![]() |
UseOnlyOverlappedIO |
指定套接字是否应仅使用重叠的 I/O 模式。 |
下面的代码示例演示如何 Socket 类可以用于将数据发送到 HTTP 服务器并接收响应。 此示例中受到阻止,直到收到整个页面。
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);
}
}




