我什么时候需要绑定套接字?

时间:2012-01-30 00:42:08

标签: c# sockets

何时需要绑定UDP套接字? 此代码抛出一个异常,说我需要先绑定套接字

// server
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint remote = new IPEndPoint(IPAddress.Any, 0)
byte[] buffer = new byte[1024];
sock.ReceiveFrom(buffer, ref remote);

而这个不是

// client
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint server = new IPEndPoint(IPAddress.Parse("server ip"), 1234);
byte[] toSend = { 1, 2, 3 };
sock.SendTo(toSend, server);
byte[] buffer = new byte[1024];
sock.ReceiveFrom(buffer, ref server);

我有一些理论,但似乎没有一个理论合理。

0 个答案:

没有答案