开发者

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜