2012-01-23 29 views
0

我需要知道如何檢查IP端口是否正在連接到。 端口是7171,我正在使用Visual Studio C#Express 2010 .NET。檢查帶端口的IP是否可用?

+0

嘗試谷歌?這裏:http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx – mtijn

+0

你爲什麼要這樣做?你有沒有試過打開連接? – svick

+1

@mtijn,ping不能用於特定的端口,只是IP地址。 – svick

回答

4

要檢查ip是否正在工作,您可以使用您的代碼執行ping命令並從您的代碼中打開cmd。

您可以檢查端口是免費的假設你使用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; 
    } 
}