Socket 类为网络通信提供了一套丰富的方法和属性。Socket 类允许您使用 ProtocolType 枚举中所列出的任何一种协议执行异步和同步数据传输。
Socket类对异步方法遵循 .NET Framework 命名模式。例如,同步的 Receive 方法对应于异步的 BeginReceive 和 EndReceive 方法。
如果应用程序在执行期间只需要一个线程,请使用下面的方法,这些方法适用于同步操作模式。
如果当前使用的是面向连接的协议(如 TCP),则服务器可以使用 Listen 方法侦听连接。Accept 方法处理任何传入的连接请求,并返回可用于与远程主机进行数据通信的 Socket。可以使用此返回的 Socket 来调用 Send 或 Receive 方法。如果要指定本地 IP 地址和端口号,请在调用 Listen 方法之前先调用 Bind 方法。如果您希望基础服务提供程序为您分配可用端口,请使用端口号 0。如果希望连接到侦听主机,请调用 Connect 方法。若要进行数据通信,请调用 Send 或 Receive 方法。
如果当前使用的是无连接协议(如 UDP),则根本不需要侦听连接。调用 ReceiveFrom 方法可接受任何传入的数据报。使用 SendTo 方法可将数据报发送到远程主机。
若要在执行过程中使用单独的线程处理通信,请使用下面的方法,这些方法适用于异步操作模式。
如果当前使用的是面向连接的协议(如 TCP),则可使用 Socket、BeginConnect 和 EndConnect 方法来连接侦听主机。通过使用 BeginSend 和 EndSend 方法,或者使用 BeginReceive 和 EndReceive 方法,可以进行异步数据通信。可以使用 BeginAccept 和 EndAccept 处理传入的连接请求。
如果您使用的是 UDP 等无连接协议,则可以使用 BeginSendTo 和 EndSendTo 来发送数据报,而使用 BeginReceiveFrom 和 EndReceiveFrom 来接收数据报。
如果对一个套接字执行多个异步操作,它们不一定按启动时的顺序完成。
当数据发送和数据接收完成之后,可使用 Shutdown方法来禁用 Socket。在调用 Shutdown 之后,可调用 Close 方法来释放与 Socket 关联的所有资源。
通过 Socket类,您可以使用 SetSocketOption 方法来配置 Socket。可以使用 GetSocketOption 方法来检索这些设置。
小结一下:
服务器使用 Listen 方法侦听连接,用Accept 方法处理任何传入的连接请求,使用Connect 方法接到侦听主机,并返回可用于远程主机进行数据通信的Socket,一般进行通信调用Socket的Send和Receive方法。
Socket 构造函数 (AddressFamily, SocketType, ProtocolType)
addressFamily 参数指定 Socket 类使用的寻址方案,socketType 参数指定 Socket 类的类型,protocolType 参数指定 Socket 使用的协议。这三个参数不是独立的。有些地址族会限制哪些协议可与其一起使用,同时 Socket 类型在协议中通常是隐式的。如果地址族、Socket 类型和协议类型的组合导致无效的 Socket,则此构造函数将引发 SocketException。
{
// Define those variables to be evaluated in the next for loop and
// then used to connect to the server. These variables are defined
// outside the for loop to make them accessible there after.
Socket s = null;
IPEndPoint hostEndPoint; //IPEndPoint将网络端点表示为 IP 地址和端口号。
IPAddress hostAddress = null;
int conPort = 80;
// Get DNS host information.
IPHostEntry hostInfo = Dns.GetHostEntry(server);
// Get the DNS IP addresses associated with the host.
IPAddress[] IPaddresses = hostInfo.AddressList;
// Evaluate the socket and receiving host IPAddress and IPEndPoint.
for (int index=0; index<IPaddresses.Length; index++)
{
hostAddress = IPaddresses[index];
hostEndPoint = new IPEndPoint(hostAddress, conPort);
// Creates the Socket to send data over a TCP connection.
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
// Connect to the host using its IPEndPoint.
s.Connect(hostEndPoint);
if (!s.Connected)
{
// Connection failed, try next IPaddress.
strRetPage = "Unable to connect to host";
s = null;
continue;
}
// Sent the GET request to the host.
s.Send(ByteGet, ByteGet.Length, 0);
} // End of the for loop.
// Receive the host home page content and loop until all the data is received.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); //使用指定的 SocketFlags ,从绑定的 Socket 接收指定字节数的数据,并将数据存入接收缓冲区。
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
} // End of the try block.
catch(SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(NullReferenceException e)
{
Console.WriteLine("NullReferenceException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
return strRetPage;
}
public static void Main()
{
Console.WriteLine(DoSocketGet("localhost"));
}
}
Socket
如果应用程序在执行期间只需要一个线程,请使用下面的方法,这些方法适用于同步操作模式。
若要在执行过程中使用单独的线程处理通信,请使用下面的方法,这些方法适用于异步操作模式。
如果对一个套接字执行多个异步操作,它们不一定按启动时的顺序完成。
当数据发送和数据接收完成之后,可使用 Shutdown
通过 Socket
小结一下:
服务器使用 Listen 方法侦听连接,用Accept 方法处理任何传入的连接请求,使用Connect 方法接到侦听主机,并返回可用于远程主机进行数据通信的Socket,一般进行通信调用Socket的Send和Receive方法。
Socket 构造函数
addressFamily 参数指定
{
// Define those variables to be evaluated in the next for loop and
// then used to connect to the server. These variables are defined
// outside the for loop to make them accessible there after.
Socket s = null;
IPEndPoint hostEndPoint; //IPEndPoint将网络端点表示为 IP 地址和端口号。
IPAddress hostAddress = null;
int conPort = 80;
// Get DNS host information.
IPHostEntry hostInfo = Dns.GetHostEntry(server);
// Get the DNS IP addresses associated with the host.
IPAddress[] IPaddresses = hostInfo.AddressList;
// Evaluate the socket and receiving host IPAddress and IPEndPoint.
for (int index=0; index<IPaddresses.Length; index++)
{
hostAddress = IPaddresses[index];
hostEndPoint = new IPEndPoint(hostAddress, conPort);
// Creates the Socket to send data over a TCP connection.
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
// Connect to the host using its IPEndPoint.
s.Connect(hostEndPoint);
if (!s.Connected)
{
// Connection failed, try next IPaddress.
strRetPage = "Unable to connect to host";
s = null;
continue;
}
// Sent the GET request to the host.
s.Send(ByteGet, ByteGet.Length, 0);
} // End of the for loop.
// Receive the host home page content and loop until all the data is received.
Int32 bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); //使用指定的 SocketFlags
strRetPage = "Default HTML page on " + server + ":\r\n";
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
while (bytes > 0)
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
}
} // End of the try block.
catch(SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(NullReferenceException e)
{
Console.WriteLine("NullReferenceException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch(Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
return strRetPage;
}
public static void Main()
{
Console.WriteLine(DoSocketGet("localhost"));
}
}
浙公网安备 33010602011771号