我有两个inputBoxes,IP/地址和端口。
我搜索一个简单的代码,它可以检查服务器(地址+端口)是在线还是离线。结果将显示在例如标签中(在线/离线)。
服务器检查可以使用计时器或按钮。
发布于 2015-06-06 05:00:10
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect("192.168.0.1", 22);
Console.WriteLine("Port open");
}
catch (Exception)
{
Console.WriteLine("Port closed");
}此代码片段将用于测试使用TCP协议侦听给定IP和端口的服务器。
此外,您还可以尝试ping该IP:
Ping ping = new Ping();
PingReply pingReply = ping.Send("192.168.0.200");
if (pingReply.Status == IPStatus.Success)
{
//Server is alive
}Ping类位于System.Net.NetworkInformation中。
https://stackoverflow.com/questions/30675646
复制相似问题