C# ping操作,MSDN版(不是socket自己写值的版本)
来源:互联网 发布:西岐网络新三板 编辑:程序博客网 时间:2024/06/05 07:22
class PingClass {
private static long ms = -1;
public static long ping(string ip) {
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 = 2000;
// 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(ip, 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.");
return ms;
}
private 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);
ms = reply.RoundtripTime;
} else {
ms = -1;
}
}
}
0 0
- C# ping操作,MSDN版(不是socket自己写值的版本)
- 自己写ping的实现
- 自己写的Ping 程序
- 自己写的一个ping程序
- 自己写的一个ping程序
- 今天终于搞清了Serializable ,虽然不是自己写的程序!(去马赛克版)
- C#写的一个ping程序
- C# 自己重新写的FTP操作类
- 自己写的RxBus,写的不是很好勿喷
- c#写Socket(一)
- C#写Socket(二)
- 自己写的单链表操作
- 自己写的socket 多线程 通讯
- 自己写的socket通信代码
- 有关线程的一道题(自己写的,不是很好,为了清电脑)
- 数位DP模板(感觉好神奇) 看大神的 不是自己写的
- 挺不错的验证码(代码不是自己写的)
- 关于读不是自己写的程序的问题
- c# 通过快捷方式读取目标路径
- dwz.js表格自动适应宽度
- Windows安装Apache 2.2.17、PHP 5.3.5与MySQL
- Activiti博客资源贴
- WebSocket发送消息,大于126字节和大于65535字节的处理
- C# ping操作,MSDN版(不是socket自己写值的版本)
- Ubuntu无法安装rpm包,ubuntu RPM should not be used directly install RPM packages, use Alien instead!
- 将博客搬至CSDN
- FireBug,Chrome调试工具,动态加载jQuery库或其他库代码
- uav10161
- Xcode 7真机免证书调试
- 计算机cpu、寄存器、内存区别
- 性能测试过程中前端某图片显示异常问题及解决思路
- Clustering by density peaks and distance