C# 在网络上Ping主机名
如何在网络上ping主机名 我将使用该类。System.Net.NetworkInformation命名空间就是您所需要的。 这有更多的细节C# 在网络上Ping主机名,c#,.net,ping,C#,.net,Ping,如何在网络上ping主机名 我将使用该类。System.Net.NetworkInformation命名空间就是您所需要的。 这有更多的细节 public static string PingHost(string host) { //string to hold our return messge string returnMessage = string.Empty; //IPAddress instance for holding the returned hos
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={2}ms 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;
}
公共静态字符串PingHost(字符串主机)
{
//字符串来保存我们的返回消息
string returnMessage=string.Empty;
//用于保存返回主机的IPAddress实例
IPAddress地址=GetIpFromHost(参考主机);
//设置ping选项,TTL 128
PingOptions PingOptions=新的PingOptions(128,true);
//创建一个新的ping实例
Ping Ping=新Ping();
//32字节缓冲区(创建空)
字节[]缓冲区=新字节[32];
//首先确保我们确实有一个互联网连接
if(HasConnection())
{
//这里我们将ping主机4次(标准)
对于(int i=0;i<4;i++)
{
尝试
{
//向主机发送ping 4次,并记录返回的数据。
//Send()方法需要4项:
//1) 我们正在ping的IP地址
//2) 超时值
//3) 缓冲区(我们的字节数组)
//4) PingOptions
PingReply PingReply=ping.Send(地址,1000,缓冲区,pingOptions);
//确保我们没有空的回复
如果(!(pingReply==null))
{
开关(pingReply.Status)
{
案例IPStatus.成功:
returnMessage=string.Format(“从{0}回复:字节={1}时间={2}ms TTL={3}”,pingReply.Address,pingReply.Buffer.Length,pingReply.RoundtripTime,pingReply.Options.TTL);
打破
case IPStatus.TimedOut:
returnMessage=“连接已超时…”;
打破
违约:
returnMessage=string.Format(“Ping失败:{0}”,pingReply.Status.ToString());
打破
}
}
其他的
returnMessage=“连接因未知原因失败…”;
}
捕获(PingEx异常)
{
returnMessage=string.Format(“连接错误:{0}”,例如Message);
}
捕获(SocketException例外)
{
returnMessage=string.Format(“连接错误:{0}”,例如Message);
}
}
}
其他的
returnMessage=“未找到Internet连接…”;
//回信
返回消息;
}
试试这个
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);
}
}
}
}
回想起来,你让它看起来很明显。是的,我知道。我等待确认答案被接受后才发布。请参考您的代码片段。这只是MSDN链接的一个副本。能否在此处包含GetIpFromHost方法的源代码?(我知道它在链接中,但是没有它,答案就不起作用了。)谢谢。实际上,仔细检查后,它看起来像是将这一行替换为:“var address=Dns.GetHostEntry(host.AddressList.First();”就够了——而且它不会再吞下有价值的异常。HasConnection()未定义。似乎对于IP v6地址,pingReply。选项为nullWorked for me,新Uri(主机)。主机为主机地址,HasConnection()为System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()。顺便说一句,IMO返回pingStatus和抛出异常更好。