public PingReply Send(IPAddress address, int timeout);在VS中导航到其定义,有如下说明:
// 摘要:
// 试图将包含指定数据缓冲区的 Internet 控制消息协议 (ICMP) 回送消息发送到具有指定的 System.Net.IPAddress 的计算机,并接收来自该计算机的相应
//ICMP 回送应答消息。使用此方法可以为操作指定一个超时值。问题:“包含指定数据缓冲区的 Internet 控制消息协议 (ICMP)”这里的ICMP报文从哪里来?我看过一些实例程序,里面都没有涉及到ICMP报文的构造,是不是Ping类已经封装好的了?
{
Ping pingSender = new Ping (); // Create a buffer of 32 bytes of data to be transmitted.
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes (data); // Wait 10 seconds for a reply.
int timeout = 10000; // Set options for transmission:
// The data can go through 64 gateways or routers
// before it is destroyed, and the data packet
// cannot be fragmented.
PingOptions options = new PingOptions (64, true); // Send the request.
PingReply reply = pingSender.Send ("www.contoso.com", 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);
}
else
{
Console.WriteLine (reply.Status);
}
}