Socket.SetSocketOption throws invalid argument exception on Mac OS & Linux ,but works fine on Windows

Viewed 752
Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, timeout);

If timeout is set to -1, then the method works fine on Windows, but fails on Linux & Mac with the following exception

System.Net.Sockets.SocketException (22): Invalid argument at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName) at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue, Boolean silent) at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue)

If timeout is set to 0 on mac and linux, there is no exception.

1) if timeout is set to -1 it does a indefinite poll on windows. Will timeout=0 do the same on windows?

2) ON linux if timeout is set to 0? Does it do a indefinite poll?

When i use 0 for timeout then the application intermittently fails with

    System.NotSupportedException: The BeginWrite method cannot be called >when another write operation is pending.
      at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, >Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
      at System.Net.Security._SslStream.BeginWrite(Byte[] buffer, Int32 >offset, Int32 count, AsyncCallback asyncCallback, Object asyncState)
1 Answers

1) if timeout is set to -1 it does a indefinite poll on windows. Will timeout=0 do the same on windows?

2) ON linux if timeout is set to 0? Does it do a indefinite poll?

Yes, the values of 0 and -1 both have the same behaviour according to the documentation for Socket.RecieveTimeout

The time-out value, in milliseconds. The default value is 0, which indicates an infinite time-out period. Specifying -1 also indicates an infinite time-out period.

Related