【问题标题】:TCP/IP Port stops listening all of suddenTCP/IP 端口突然停止监听
【发布时间】:2019-02-19 11:51:25
【问题描述】:

小背景

我有一个 Windows 服务(用 C# .NET 编写),它使用 TcpListener 在几个不同的端口上进行侦听。一些端口是(8090、8091、554、25100、25101)。 每个端口侦听器执行不同的任务,我不会在此问题中详细提及。 该服务连续数周正常运行,没有任何问题,然后突然端口 8091 之一停止接收新的客户端连接。但是,其他端口仍然可以正常运行,Windows 服务也是如此。

如何使用此端口 (8091)

用户可以使用安全的 HTTPS URL 从浏览器访问 Windows 服务端口。例如看看这个 URL。

https://XYZServer.net:8091/ABC/?access=123

在 Windows 服务中,我使用 TCPListner 打开此端口。下面写了一些示例代码。

m_listener = new TcpListener(IPAddress.Any, 8091);
m_listener.Start();

m_listener.BeginAcceptTcpClient(DoAcceptWebTcpClientCallback, m_listener);

然后当一个新的客户端连接请求进来时。查看一些示例代码。

private void DoAcceptWebTcpClientCallback(IAsyncResult ar)
        {
            try
            {
                var client = m_listener.EndAcceptTcpClient(ar);

                var webConnection = CreateConnection(client, m_waitForDeviceTimeout);
                webConnection.Start();
            }
            catch (Exception ex)
            {
                s_log.Error(ex, "{0}: Exception", m_serverName);
            }

            try
            {
                BeginAccept();
            }
            catch (Exception ex)
            {
                s_log.Error(ex, "{0}: Exception", m_serverName);
            }
        }

我将 sslStream 与客户端连接相关联。此外,我正在使用证书对服务器进行身份验证,而不对客户端进行身份验证。下面的示例代码

protected override WebConnection CreateConnection(TcpClient client, int waitForFb4Timeout)
        {
            return new HttpsConnection(SERVER_NAME, client, m_serverCertificate, waitForFb4Timeout); // Create an ssl/tls connection using the configured certificate
        }

public HttpsConnection(string serverName, TcpClient client, X509Certificate serverCertificate, int waitForFb4Timeout) : 
            base (serverName, client, GetStream(client, serverCertificate), DeviceConnection.ProtocolEnum.HTTP, waitForFb4Timeout)
        {
        }

        private static Stream GetStream(TcpClient client, X509Certificate serverCertificate)
        {
            // Create the SslStream using the client's network stream.
            var sslStream = new SslStream(client.GetStream(), false);

            // Authenticate the server but don't require the client to authenticate.
            sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true);

            return sslStream;
        }

Windows 服务将来自此端口的所有消息中继到另一个目的地(WebSocker 服务器)并将响应中继回浏览器。然后用户可以在屏幕上看到该页面。这在几周内一直正常工作,然后突然间用户无法浏览该页面,并且端口 8091 上的新客户端连接请求停止出现。

发生这种情况时,不会引发异常。真的很烦人,很难理解这里到底出了什么问题..

还有一件事我想提一下,在同一个 Windows 服务中,我们正在建立大量使用临时端口范围 (49000-65535) 的出站连接。我可以看到所有这些端口都被使用,然后一次又一次地重复使用。但是,没有证据表明所有端口都被同时使用。我知道有一些方法可以增加端口范围,但我还没有尝试过,因为我不相信我们已经达到了极限。

我可以在与此端口上的身份验证相关的代码中看到几个异常。

2018-08-11 01:39:43.8952|ERROR|Fb4RelayServerLib.WebConnections.WebServer.DoAcceptWebTcpClientCallback: HTTPS: Exception System.IO.IOException: Authentication failed because the remote party has closed the transport stream.

人们在不同的论坛上提出了关于使用的建议

SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12

而我只使用 SslProtocols.Tls(见代码)。我没有在上面尝试过,因为在我的情况下,我很少遇到此异常,并且对于相同的客户端 IP,它也可以正常工作。

我真的很困惑,不知道如何解决和进一步调查这个问题。在这方面的任何帮助都将非常有帮助。非常感谢您提出一个冗长的问题。

谢谢

【问题讨论】:

    标签: c# tcp port


    【解决方案1】:

    好吧,经过一番努力,我终于解决了这个问题。

    我主要做了两件事来解决这个问题。

    1- 添加对我的产品最初缺乏的 tls1.1 和 tls1.2 的支持。 (正如我在我的问题中发布的那样)

    2- 将 AuthenticateAsServer 替换为 AuthenticateAsserverAsync。

    实际上,由于某种未知原因,AuthenticateAsServer 函数挂起,不允许应用程序接受任何新的客户端连接。

    进行上述更改,到目前为止似乎工作正常。保持我的手指交叉:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-27
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多