开发者

Does Socket ReceiveAsync support UDP?

开发者 https://www.devze.com 2023-02-27 08:18 出处:网络
I have seen examples of ReceiveAsync for TCP, but not UDP.开发者_C百科Is ReceiveAsync compatible with UDP? If so, have you seen any examples?

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

精彩评论

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