当我使用Windows Ping远程系统时,它说没有回复,但是当我用c#ping时,它表示成功。 Windows正确,设备未连接。为什么我的代码能够在Windows不能成功ping时?
这是我的代码:
Ping p1 = new Ping();
PingReply PR = p1.Send("192.168.2.18");
// check when the ping is not success
while (!PR.Status.ToString().Equals("Success"))
{
Console.WriteLine(PR.Status.ToString());
PR = p1.Send("192.168.2.18");
}
// check after the ping is n success
while (PR.Status.ToString().Equals("Success"))
{
Console.WriteLine(PR.Status.ToString());
PR = p1.Send("192.168.2.18");
}
答案 0 :(得分:188)
using System.Net.NetworkInformation;
public static bool PingHost(string nameOrAddress)
{
bool pingable = false;
Ping pinger = null;
try
{
pinger = new Ping();
PingReply reply = pinger.Send(nameOrAddress);
pingable = reply.Status == IPStatus.Success;
}
catch (PingException)
{
// Discard PingExceptions and return false;
}
finally
{
if (pinger != null)
{
pinger.Dispose();
}
}
return pingable;
}
答案 1 :(得分:27)
在C#中使用ping是通过使用方法Ping.Send(System.Net.IPAddress)
实现的,该方法对提供的有效IP地址或URL运行ping请求,并获得一个响应的响应,其中包含一个名为Internet Control Message Protocol (ICMP) Packet的响应包含来自收到ping请求的ping服务器的响应数据的20个字节的头,.Net framework System.Net.NetworkInformation
命名空间包含一个名为PingReply
的类,其类具有用于转换{{1}的属性响应并提供有关ping服务器网络的有用信息,例如:
以下是一个使用ICMP
来演示ping如何在c#中工作的简单示例,通过在WinForms
中提供有效的IP地址并单击textBox1
,我们正在创建{的实例{1}},一个局部变量button1
,用于存储IP或URL地址的局部变量字符串,然后我们将我们创建的Ping class
局部变量分配给ping Send方法,然后我们检查是否通过比较回复状态PingReply
状态来请求成功,接下来我们从PingReply
局部变量中提取我们需要为用户显示的信息,如上所述:
IPAddress.Success
答案 2 :(得分:2)
导入System.Net.NetworkInformation
公共函数PingHost(ByVal名称或地址为字符串)为布尔值 可调光为布尔=假 暗淡的坪 Dim lPingReply作为PingReply
Try
pinger = New Ping()
lPingReply = pinger.Send(nameOrAddress)
MessageBox.Show(lPingReply.Status)
If lPingReply.Status = IPStatus.Success Then
pingable = True
Else
pingable = False
End If
Catch PingException As Exception
pingable = False
End Try
Return pingable
End Function
答案 3 :(得分:0)
private async void Ping_Click(object sender, RoutedEventArgs e)
{
Ping pingSender = new Ping();
string host = @"stackoverflow.com";
await Task.Run(() =>{
PingReply reply = pingSender.Send(host);
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("Address: {0}", reply.Status);
}
});
}
答案 4 :(得分:-5)
private void button26_Click(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = @"C:\windows\system32\cmd.exe";
proc.Arguments = "/c ping -t " + tx1.Text + " ";
System.Diagnostics.Process.Start(proc);
tx1.Focus();
}
private void button27_Click(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo proc = new System.Diagnostics.ProcessStartInfo();
proc.FileName = @"C:\windows\system32\cmd.exe";
proc.Arguments = "/c ping " + tx2.Text + " ";
System.Diagnostics.Process.Start(proc);
tx2.Focus();
}