【问题标题】:How to Free a Port in .NET....after a TCPListener has recieved a required thing from remote client如何在 .NET 中释放端口....在 TCPListener 从远程客户端收到所需内容后
【发布时间】:2017-08-17 22:22:22
【问题描述】:

如何在 .NET 中释放 TCPListener 使用的端口.... 在 TCPListener 从

收到所需的东西之后

【问题讨论】:

    标签: .net port tcplistener


    【解决方案1】:

    只需调用 TcpListener 上的 Stop 方法。

    【讨论】:

      【解决方案2】:

      我们首先建立了第一个Socket:

           Socket socket1; 
           IPEndPoint localEP = new IPEndPoint (IPAddress.Any, 20000); 
           Socket1 = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
           Socket1.Bind (localEP); 
      

      然后是第二个 Socket:

           Socket socket2 
           IPEndPoint localEP = new IPEndPoint (IPAddress.Any, 20000); 
           Socket2 = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 
           Socket2.SetSocketOption (SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 
          / / Please note this one. True ReuseAddress options will be set to allow the socket to bind already in use in the address. 
           Socket2.Bind (localEP); 
      

      这样会将Socket1和Socket2捆绑在同一个端口上。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-23
        • 2012-03-31
        • 2014-01-23
        • 2011-01-25
        • 1970-01-01
        • 2010-11-21
        相关资源
        最近更新 更多