ping 网络上的主机名
Posted
技术标签:
【中文标题】ping 网络上的主机名【英文标题】:Ping a hostname on the network 【发布时间】:2011-04-11 00:33:41 【问题描述】:如何 ?
【问题讨论】:
【参考方案1】:System.Net.NetworkInformation 命名空间是您所需要的。 这个link有更多细节
public static string PingHost(string host)
//string to hold our return messge
string returnMessage = string.Empty;
//IPAddress instance for holding the returned host
IPAddress address = GetIpFromHost(ref host);
//set the ping options, TTL 128
PingOptions pingOptions = new PingOptions(128, true);
//create a new ping instance
Ping ping = new Ping();
//32 byte buffer (create empty)
byte[] buffer = new byte[32];
//first make sure we actually have an internet connection
if (HasConnection())
//here we will ping the host 4 times (standard)
for (int i = 0; i < 4; i++)
try
//send the ping 4 times to the host and record the returned data.
//The Send() method expects 4 items:
//1) The IPAddress we are pinging
//2) The timeout value
//3) A buffer (our byte array)
//4) PingOptions
PingReply pingReply = ping.Send(address, 1000, buffer, pingOptions);
//make sure we dont have a null reply
if (!(pingReply == null))
switch (pingReply.Status)
case IPStatus.Success:
returnMessage = string.Format("Reply from 0: bytes=1 time=2ms TTL=3", pingReply.Address, pingReply.Buffer.Length, pingReply.RoundtripTime, pingReply.Options.Ttl);
break;
case IPStatus.TimedOut:
returnMessage = "Connection has timed out...";
break;
default:
returnMessage = string.Format("Ping failed: 0", pingReply.Status.ToString());
break;
else
returnMessage = "Connection failed for an unknown reason...";
catch (PingException ex)
returnMessage = string.Format("Connection Error: 0", ex.Message);
catch (SocketException ex)
returnMessage = string.Format("Connection Error: 0", ex.Message);
else
returnMessage = "No Internet connection found...";
//return the message
return returnMessage;
【讨论】:
您能否在此处包含 GetIpFromHost 方法的源代码? (我知道它在链接中,但如果没有它,答案就无法真正起作用。)谢谢。 实际上,经过仔细检查,它看起来像是用以下内容替换该行:“var address = Dns.GetHostEntry(host).AddressList.First();”已经足够好了——它不会再吞下有价值的例外。 HasConnection() 未定义。 似乎对于 IP v6 地址,pingReply.Options 为空 为我工作,新的 Uri(host).Host 是主机地址,HasConnection() 是 System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()。 BTW IMO 返回 pingStatus 并抛出异常更好。【参考方案2】:我会使用Ping 类。
【讨论】:
回想起来,你让它看起来很明显。 是的,我知道。不过,我在发布之前等待确保答案被接受。【参考方案3】:试试这个
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace Examples.System.Net.NetworkInformation.PingTest
public class PingExample
// args[0] can be an IPaddress or host name.
public static void Main (string[] args)
Ping pingSender = new Ping ();
PingOptions options = new PingOptions ();
// Use the default Ttl value which is 128,
// but change the fragmentation behavior.
options.DontFragment = true;
// Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data);
int timeout = 120;
PingReply reply = pingSender.Send (args[0], timeout, buffer, options);
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);
Console.WriteLine ("Don't fragment: 0", reply.Options.DontFragment);
Console.WriteLine ("Buffer size: 0", reply.Buffer.Length);
【讨论】:
请参考您的代码sn-p。这只是 MSDN 链接的副本。以上是关于ping 网络上的主机名的主要内容,如果未能解决你的问题,请参考以下文章