问题描述
我必须检查远程IP和端口是否可用.如果可用它会移动到下一个表单.如果不可用它应该进入初始状态.我尝试使用这个
I have to check remote IP and Port is available or not.If its is available it will move to next form.If not available it should come to the initial state.I tried using this
while (true)
{
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners();
-------
-------
-------
}
我正在展示示例编码.它正在检查本地 IP 和端口并移动到下一个表单.它将检查本地端口和 IP 是否可用.如果端口和 IP 不可用,它将进入初始阶段并且它正在工作很好.同样的事情我必须检查远程端口和 IP.
I am showing the example coding.it was checking local IP and port and moving to next form.it will check local port and IP is available.if port and IP not available it will come to the initial stage and it was working fine.same thing i have to check in remote Port and IP.
推荐答案
使用.NET的Ping
类来查明系统是否启动并连接,使用PortScanner
检查端口是否打开.检查这些链接以进一步阅读和探索.
Use the Ping
class of .NET to find out if the system is up and connected, the use the PortScanner
to check if the port is open. check these links for further reading and exploring.
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs.110%29.aspx
或
public static bool PingHost(string hostUri, int portNumber)
{
try
{
using (var client = new TcpClient(hostUri, portNumber))
return true;
}
catch (SocketException ex)
{
MessageBox.Show("Error pinging host:'" + hostUri + ":" + portNumber.ToString() + "'");
return false;
}
}
这篇关于如何检查远程IP和端口是否可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!