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.
Two of these options are not supported before Windows 1709, namely
TcpKeepAliveTimeandTcpKeepAliveInterval. AndTcpKeepAliveRetryCountis only available beginning 1703. Windows Server 2016 is equivalent to Windows 10 version 1607.The Winsock documentation says under the
TCP_KEEPIDLEandTCP_KEEPINTVLoptions:And for
TCP_KEEPCNTit says:You will therefore just have to use a
trycatchas there appears to be no way to check supported options.