Checking if ip with port is available?

2020-05-06 12:17发布

问题:

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:

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;
   }
 }


标签: c# ip port