Does Socket ReceiveAsync support UDP?
I have seen examples of ReceiveAsync for TCP, but not UDP.开发者_C百科 Is ReceiveAsync compatible with UDP? If so, have you seen any examples?
Thanks.
UDP is a datagram-oriented protocol, so you'd use the Socket.ReceiveFromAsync Method.
Example:
var s = new Socket(SocketType.Dgram, ProtocolType.Udp);
s.Bind(localEP);
var e = new SocketAsyncEventArgs();
e.Completed += OnReceive;
e.RemoteEndPoint = new IPEndPoint(IPAddress.IPv6Any, 0);
e.SetBuffer(new byte[BufferSize], 0, BufferSize);
if (!s.ReceiveFromAsync(e)) OnReceive(s, e);
精彩评论