Checking if ip with port is available?

后端 未结 1 414
青春惊慌失措
青春惊慌失措 2021-01-26 04:27

I need to know how to check if an IP with Port is working to connect to. Port is 7171, and I\'m using Visual Studio C# Express 2010 .NET.

相关标签:
1条回答
  • 2021-01-26 05:27

    To check ip is working you can do a ping using your code and opening cmd from your code.

    You can check if port is free assuming you are using tcpclint :

    int port = 456; //<--- This is your value
    bool isAvailable = true;
    
    IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties();
     TcpConnectionInformation[] tcpConnInfoArray = ipGlobalProperties.GetActiveTcpConnections();
    
     foreach (TcpConnectionInformation tcpi in tcpConnInfoArray)
     {
       if (tcpi.LocalEndPoint.Port==port)
       {
         isAvailable = false;
         break;
       }
     }
    
    0 讨论(0)
提交回复
热议问题