ping 计算机,如何:Ping 主机

如何:Ping 主机

03/30/2017

本文内容

此示例演示如何 ping 远程主机。

示例

using System;

using System.Text;

using System.Net;

using System.Net.NetworkInformation;

using System.ComponentModel;

using System.Threading;

namespace Examples.System.Net.NetworkInformation.PingTest

{

public class PingExample

{

public static void Main (string[] args)

{

if (args.Length == 0)

throw new ArgumentException ("Ping needs a host or IP Address.");

string who = args[0];

AutoResetEvent waiter = new AutoResetEvent (false);

Ping pingSender = new Ping ();

// When the PingCompleted event is raised,

// the PingCompletedCallback method is called.

pingSender.PingCompleted += new PingCompletedEventHandler (PingCompletedCallback);

// Create a buffer of 32 bytes of data to be transmitted.

string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

byte[] buffer = Encoding.ASCII.GetBytes (data);

// Wait 12 seconds for a reply.

int timeout = 12000;

// 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);

Console.WriteLine ("Time to live: {0}", options.Ttl);

Console.WriteLine ("Don't fragment: {0}", options.DontFragment);

// Send the ping asynchronously.

// Use the waiter as the user token.

// When the callback completes, it can wake up this thread.

pingSender.SendAsync(who, timeout, buffer, options, waiter);

// Prevent this example application from ending.

// A real application should do something useful

// when possible.

waiter.WaitOne ();

Console.WriteLine ("Ping example completed.");

}

public static void PingCompletedCallback (object sender, PingCompletedEventArgs e)

{

// If the operation was canceled, display a message to the user.

if (e.Cancelled)

{

Console.WriteLine ("Ping canceled.");

// Let the main thread resume.

// UserToken is the AutoResetEvent object that the main thread

// is waiting for.

((AutoResetEvent)e.UserState).Set ();

}

// If an error occurred, display the exception to the user.

if (e.Error != null)

{

Console.WriteLine ("Ping failed:");

Console.WriteLine (e.Error.ToString ());

// Let the main thread resume.

((AutoResetEvent)e.UserState).Set();

}

PingReply reply = e.Reply;

DisplayReply (reply);

// Let the main thread resume.

((AutoResetEvent)e.UserState).Set();

}

public static void DisplayReply (PingReply reply)

{

if (reply == null)

return;

Console.WriteLine ("ping status: {0}", reply.Status);

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);

}

}

}

}

编译代码

此示例需要:

引用 System.Net 命名空间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值