Socket ReceiveAsync是否支持UDP?

时间:2011-04-19 21:42:44

标签: c# sockets asynchronous udp

我见过TCP的ReceiveAsync示例,但没有看到UDP。 ReceiveAsync是否与UDP兼容?如果是这样,你看过任何一个例子吗?

感谢。

1 个答案:

答案 0 :(得分:5)

UDP是一种面向数据报的协议,因此您可以使用Socket.ReceiveFromAsync Method

示例:

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