我有两个inputBoxes,IP /地址和端口.
我搜索一个简单的代码,可以检查服务器(地址+端口)是在线还是离线.结果将显示在例如标签(在线/离线)中.
服务器检查可以使用计时器或按钮.
Ola*_*cea 21
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect("192.168.0.1", 22);
Console.WriteLine("Port open");
}
catch (Exception)
{
Console.WriteLine("Port closed");
}
Run Code Online (Sandbox Code Playgroud)
此代码段将用于测试使用TCP协议侦听给定IP和端口的服务器.
此外,您还可以尝试ping IP:
Ping ping = new Ping();
PingReply pingReply = ping.Send("192.168.0.200");
if (pingReply.Status == IPStatus.Success)
{
//Server is alive
}
Run Code Online (Sandbox Code Playgroud)
Ping班级位于System.Net.NetworkInformation.
| 归档时间: |
|
| 查看次数: |
15843 次 |
| 最近记录: |