c#如何用socket去ping通一个已知IP地址的设备呢?比如该设备IP为192.168.42.150,电脑的IP为192.168.42.1
1个回答
展开全部
.net 中已经自带了ping 类,如无必要否则直接使用即可
//using System.Net.NetworkInformation;
using (var ping = new Ping())
{
var pingReply = ping.Send("192.168.1.1");
if (pingReply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", pingReply.Address.ToString());
Console.WriteLine("RoundTrip time: {0}", pingReply.RoundtripTime);
Console.WriteLine("Time to live: {0}", pingReply.Options.Ttl);
Console.WriteLine("Don't fragment: {0}", pingReply.Options.DontFragment);
Console.WriteLine("Buffer size: {0}", pingReply.Buffer.Length);
}
else
{
Console.WriteLine(pingReply.Status);
}
// .net 2 下的ping 有bug,需要转为IDisposable进行释放
((IDisposable) ping).Dispose();
}
如果想用socket 实现, 可以搜索 c# icmp
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询