Checking if ip with port is available?

2020-05-06 12:37发布

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.

标签: c# ip port
1条回答
姐就是有狂的资本
2楼-- · 2020-05-06 12:52

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;
   }
 }
查看更多
登录 后发表回答