Home > Software design >  Setting keep alive on windows server 2016 throws WSAENOPROTOOPT (10042) error
Setting keep alive on windows server 2016 throws WSAENOPROTOOPT (10042) error

Time:08-10

I am trying to set keep alive times on a connected socket and getting following exception

System.Net.Sockets.SocketException (10042): An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call.
   at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
   at System.Net.Sockets.Socket.SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, Int32 optionValue)
   at System.Net.Sockets.SocketExtensions.SetKeepAlive(Socket socket, Boolean keepAlive, Int32 keepAliveTime, Int32 keepAliveInterval, Int32 keepAliveRetryCount)
   at ServerService.Service.OnManagerConnection(IConnection sender, Socket socket)

This is the code that is being called

socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, 1);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, 1);
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, 5);

The exception is only thrown on Windows Server 2016, same code works on other Windows versions and linux.

Hope someone can shed some light where the problem could be.

CodePudding user response:

Two of these options are not supported before Windows 1709, namely TcpKeepAliveTime and TcpKeepAliveInterval. And TcpKeepAliveRetryCount is only available beginning 1703. Windows Server 2016 is equivalent to Windows 10 version 1607.

The Winsock documentation says under the TCP_KEEPIDLE and TCP_KEEPINTVL options:

"This option is available starting with Windows 10, version 1709."

And for TCP_KEEPCNT it says:

Starting with Windows 10, version 1703

You will therefore just have to use a try catch as there appears to be no way to check supported options.

  • Related