使用C#测试远程服务器上的Internet连接

时间:2013-05-24 11:32:30

标签: c# connection remote-server

是否可以编写C#代码来测试远程服务器上的Internet连接?

例如,如果我使用powerhsell这样做,我会这样做:

  

Psexec \\ MyWebServer ping www.google.com

由于

1 个答案:

答案 0 :(得分:1)

您可以使用PingPingReply课程并添加System.Net.NetworkInformation来检查:

  Ping ping = new Ping();
  //PingReply reply= ping.Send("ip address");
  PingReply reply= ping.Send("www.google.com");


  if(reply.Status == IPStatus.Success)
    {
     // Connected 

       Console.WriteLine ("Address: {0}", reply.Address.ToString ());
       Console.WriteLine ("RoundTrip time: {0}", reply.RoundtripTime);
       Console.WriteLine ("Time to live: {0}", reply.Options.Ttl);
       Console.WriteLine ("Don't fragment: {0}", reply.Options.DontFragment);
       Console.WriteLine ("Buffer size: {0}", reply.Buffer.Length);
    }
  else
    {
      Console.WriteLine (reply.Status);
    }

查看here了解详情