| 名称 | 说明 | |
|---|---|---|
| Socket(AddressFamily, SocketType, ProtocolType) |
Socket 类使用指定的地址族、 套接字类型和协议。 |
|
| Socket(SocketInformation) |
DuplicateAndClose。 |
|
| Socket(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 成员。 |
| Ttl |
Socket。 |
|
| UseOnlyOverlappedIO |
指定套接字是否应仅使用重叠的 I/O 模式。 |
此示例中受到阻止,直到收到整个页面。
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);
}
}