Home > other >  How to catch timeout in blocking socket
How to catch timeout in blocking socket

Time:05-24

I am trying to make Blocking UDP socket (server)

If data doesn't receive, the socket will hang forever.

I want to make it to 5 sec timeout.

Is there any solution?

byte[] data = new byte[1024];

socket.Bind(endpoint);         
socket.Blocking = true;

EndPoint tmpRemote = (EndPoint)sender;
while (true)
{
   try
   {
      recv = socket.ReceiveFrom(data, ref tmpRemote);
   }
}

CodePudding user response:

Socket class contain ReceiveTimeOut property . You can set any value to this property and catch exception when timeout occurs . Please check this url : https://www.demo2s.com/csharp/csharp-socket-receivetimeout.html

  • Related