jchristn / SuperSimpleTcp

Simple wrapper for TCP client and server in C# with SSL support
MIT License
459 stars 95 forks source link

keepalive option #153

Open jwj15 opened 2 years ago

jwj15 commented 2 years ago

152

I found a solution to this problem https://github.com/jchristn/SuperSimpleTcp/blob/52ba8d1aaea486a9599ac71e2aab3de7e8df1041/src/SuperSimpleTcp/SimpleTcpClient.cs#L1082

Only that option has an error in Windows 7

I suggest removing this option from win7

// win10 or later
if (Environment.OSVersion.Version.Major >= 10)
{
    _client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, _keepalive.TcpKeepAliveRetryCount); 
}

or

// win7 later
if (Environment.OSVersion.Version.Major + Environment.OSVersion.Version.Minor * 0.1 > 6.1)
{
    _client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, _keepalive.TcpKeepAliveRetryCount); 
}
jchristn commented 2 years ago

Hi @jwj15 so sorry for taking so long to get back to you on this. Looking at it now.

jchristn commented 2 years ago

Looks like we'll also have to employ RuntimeInformation.IsOSPlatform (see https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation.isosplatform?view=net-6.0) and apply the version major/minor check only against those that are running Windows.

Is it only the TcpKeepAliveRetryCount that is problematic? How about the others?

This will have to be implemented in a way that doesn't create problems when using some Linux variant that has what appears to be a version number equal to or less than Windows 7.

jwj15 commented 2 years ago

I'm not sure if it's a Windows version issue. same symptom occurred in windows10 enterprise ltsb 2016. In my case, only TcpKeepAliveRetryCount was the problem. I am using the version with only that option removed in my work environment. FYI, I work on pos and kiosk industry.

KimEoJin commented 1 year ago

@jchristn I am also having the same problem. It works fine on Windows 10. But, If a client using the 'TcpKeepAliveRetryCount' option is Executed on Windows 7, an exception message "keepalives not supported on this platform, disabled" is output and the function is immediately disconnected.

Like the solution of @jwj15, 'TcpKeepAliveRetryCount' is applied only in Windows 10 version 1703 or later, and if applied before that, an exception seems to occur. So it seems that the problem occurs in versions such as Windows 7 or windows10 enterprise ltsb 2016.

See link below.

  1. https://learn.microsoft.com/en-us/windows/win32/winsock/ipproto-tcp-socket-options#windows-support-for-ipproto_tcp-options:~:text=x-,TCP_KEEPCNT,Starting%20with%20Windows%C2%A010%2C%20version%201703,-TCP_MAXRT
  2. https://en.wikipedia.org/wiki/Windows_10,_version_1703

After applying the code below to the Local Clone Repo, the built dll was applied, and it was confirmed that it was built on Windows 7.

        private void EnableKeepalives()
        {
            // issues with definitions: https://github.com/dotnet/sdk/issues/14540

            try
            {
#if NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER

                // NETCOREAPP3_1_OR_GREATER catches .NET 5.0
                _client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
                _client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveTime, _keepalive.TcpKeepAliveTime);
                _client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveInterval, _keepalive.TcpKeepAliveInterval);

                // Windows 10 version 1703 or later
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
                    && Environment.OSVersion.Version >= new Version(10, 0, 15063))
                {
                    _client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.TcpKeepAliveRetryCount, _keepalive.TcpKeepAliveRetryCount);
                }

#elif NETFRAMEWORK