在C#控制台应用程序中,我打开了命令提示符,并检查了ping实用程序。
string aptracommand;
aptracommand = "/C ping 10.38.2.73";
Process.Start(@"cmd",aptracommand);现在,我需要应用一个条件语句,如果ping请求超时,那么它应该显示“无法连接”,如果它能够ping,则需要显示"Server is up“。
发布于 2012-09-27 20:06:43
您可以使用Ping类来实现此目的。如下所述:
using System.Net.NetworkInformation;
var ping = new Ping();
var reply = ping.Send("10.38.2.73", 60 * 1000); // 1 minute time out (in ms)
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Server is up");
}
else
{
Console.WriteLine("Server is down");
}您可以减少超时值,以便快速检查服务器是启动还是关闭。
发布于 2012-09-27 20:08:02
改用这个
Ping pingSender = new Ping ();
byte[] data = Encoding.ASCII.GetBytes ("test");
int timeout = 100;
PingReply reply = pingSender.Send("127.0.0.1", timeout, data);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address.ToString());
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
}发布于 2012-09-27 20:06:18
我认为您需要捕获输出,检查是否存在超时字符串,然后根据超时字符串做出决定。
https://stackoverflow.com/questions/12620915
复制相似问题