【问题标题】:TCP listener start exception in C#C# 中的 TCP 侦听器启动异常
【发布时间】:2012-05-29 23:35:31
【问题描述】:

我使用下面的代码创建了一个 TCP 监听器:

TCPListener = new TcpListener(IPAddress.Any, 1234);

我使用下面的代码开始监听 TCP 设备:

TCPListener.Start();

但是在这里,我无法控制端口是否在使用中。当端口在使用时,程序给出一个异常:“每个套接字地址(协议/网络地址/端口)通常只允许使用一次。”。

我该如何处理这个异常?我想警告用户该端口正在使用中。

【问题讨论】:

    标签: c# .net exception tcplistener


    【解决方案1】:

    TCPListener.Start(); 周围放置一个try/catch 块并捕获SocketException。此外,如果您要从程序中打开多个连接,那么最好在列表中跟踪您的连接并在打开连接之前查看您是否已经打开了一个连接

    【讨论】:

      【解决方案2】:

      获取异常来检查端口是否在使用中并不是一个好主意。使用 IPGlobalProperties 对象获取 TcpConnectionInformation 对象数组,然后您可以查询端点 IP 和端口。

       int port = 1234; //<--- This is your value
       bool isAvailable = true;
      
       // Evaluate current system tcp connections. This is the same information provided
       // by the netstat command line application, just in .Net strongly-typed object
       // form.  We will look through the list, and if our port we would like to use
       // in our TcpClient is occupied, we will set isAvailable to false.
       IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
       TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
      
       foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
       {
         if (tcpi.LocalEndPoint.Port==port)
         {
           isAvailable = false;
           break;
         }
       }
      
       // At this point, if isAvailable is true, we can proceed accordingly.
      

      详情请阅读this

      为了处理异常,您将按照 habib 的建议使用 try/catch

      try
      {
        TCPListener.Start();
      }
      catch(SocketException ex)
      {
        ...
      }
      

      【讨论】:

        【解决方案3】:

        捕捉它并显示您自己的错误消息。

        检查异常类型并在catch子句中使用该类型。

        try
        {
          TCPListener.Start();
        }
        catch(SocketException)
        {
          // Your handling goes here
        }
        

        【讨论】:

          【解决方案4】:

          把它放在try catch 块中。

          try {
             TCPListener = new TcpListener(IPAddress.Any, 1234);
             TCPListener.Start();
          
          } catch (SocketException e) {
            // Error handling routine
             Console.WriteLine( e.ToString());
           }
          

          【讨论】:

            【解决方案5】:

            使用 try-catch 块并捕获 SocketException。

            try
            {
              //Code here
            }
            catch (SocketException ex)
            {
              //Handle exception here
            }
            

            【讨论】:

              【解决方案6】:

              好吧,考虑到您说的是异常情况,只需使用合适的try/catch 块处理该异常,并告知用户一个事实。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2018-06-09
                • 1970-01-01
                • 2019-01-21
                • 2018-05-11
                • 2019-04-26
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多