fdh
fdh

Reputation: 5354

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? Does it keep waiting for data to arrive or does it continue? I am specifically talking about UDP Datagrams.

Upvotes: 1

Views: 1591

Answers (2)

M.Babcock
M.Babcock

Reputation: 18965

If the goal is to force your socket to stop listening after X number of seconds so you can do something else (such as checking if the application is shutting down) and you are using synchronous sockets then I would recommend setting the Socket.ReceiveTimeout to X number of seconds (times 1000 since Socket.ReceiveTimeout is in milliseconds) and then catch the resulting exception that is raised on timeout.

Upvotes: 1

dpington
dpington

Reputation: 1884

It will wait for data. If this behavior is not good enough, you can use the async recieve. Also use the UDPClient. There is no need to roll with Socket itself.

If no data is available for reading, the ReceiveFrom method will block until data is available

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

Upvotes: 2

Related Questions