How do I set read timeout value for a socket?

  • Thread starter Thread starter Peter Steele
  • Start date Start date
P

Peter Steele

If I use TCPClient, it's straightforward to set the read timeout value for
reading the associated stream:

TcpClient myClient = new TcpClient(myaddr, myport);
myClient.ReceiveTimeout = 5000;
NetworkStream myClientStream = myClient.GetStream();
myClientReader = new StreamReader(myClientStream );

However, if I've just accepted a socket connection via a listener, the
situation is a bit different. I

clientSocket = server.AcceptSocket();
NetworkStream myClientStream = new NetworkStream(clientSocket );
myClientReader = new StreamReader(myClientStream);

I'd like to do something like this:

clientSocket.ReceiveTimeout = 5000;

but there is not such method on sockets. So how can I do the equivalent
operation of setting the timeout for a socket like I can do with a TcpClient
object?
 
Use SetSocketOption

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout,
m_nMilliseconds);

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout,
m_nMilliseconds);
 
That did the trick. Thanks very much!

Peter

Bill said:
Use SetSocketOption

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout,
m_nMilliseconds);

s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout,
m_nMilliseconds);
 
Back
Top