简体   繁体   English

C#中的UDP套接字?

[英]UDP Sockets in C#?

If socket.ReceiveFrom (byte [] message, EndPoint endPoint) is used to receive data on a binded UDP socket, and no data is received what happens? 如果使用socket.ReceiveFrom (byte [] message, EndPoint endPoint)则在绑定的UDP套接字上接收数据,但是没有接收到数据,会发生什么情况? Does it keep waiting for data to arrive or does it continue? 它会一直等待数据到达还是继续? I am specifically talking about UDP Datagrams. 我专门在谈论UDP数据报。

It will wait for data. 它会等待数据。 If this behavior is not good enough, you can use the async recieve. 如果此行为不够好,您可以使用异步接收。 Also use the UDPClient. 也使用UDPClient。 There is no need to roll with Socket itself. 无需使用Socket本身。

If no data is available for reading, the ReceiveFrom method will block until data is available 如果没有可用于读取的数据,则ReceiveFrom方法将阻塞,直到数据可用

Source: http://msdn.microsoft.com/en-us/library/aa329728%28v=vs.71%29.aspx 来源: http//msdn.microsoft.com/en-us/library/aa329728%28v=vs.71%29.aspx

如果目标是强制套接字在X秒后停止侦听,以便您可以执行其他操作(例如检查应用程序是否正在关闭)并且您正在使用同步套接字,那么我建议将Socket.ReceiveTimeout设置为X秒数(自Socket.ReceiveTimeout以来的时间为1000,以毫秒为单位),然后捕获超时时引发的结果异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM