using System;
using System.Net;
using System.Text;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using System.Threading;
class GetIP{ static void Main(string[] args){
string HostName=Dns.GetHostName();
byte[] buffer = Encoding.ASCII.GetBytes ("XS");
foreach(IPAddress ip in Dns.GetHostAddresses(HostName))
{
//Console.WriteLine(ip.ToString());
try
{
Ping ping=new Ping();
ping.PingCompleted += new PingCompletedEventHandler(PingCallBackEvent);
ping.SendAsync(ip,10000);
}catch(Exception ex){
MessageBox.Show(ex.ToString());
}
}
}
public static void PingCallBackEvent(object sender,PingCompletedEventArgs e)
{
if (e.Error != null)
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ()); // Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
} if(e.Reply.Status==IPStatus.Success)
{
Console.WriteLine("连通的IP:"+e.Reply.Address.ToString());
}
//((AutoResetEvent)e.UserState).Set();
}
}
测试IP是否连通是经常报错,测试也经常不通
using System.Net;
using System.Text;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using System.Threading;
class GetIP{ static void Main(string[] args){
string HostName=Dns.GetHostName();
byte[] buffer = Encoding.ASCII.GetBytes ("XS");
foreach(IPAddress ip in Dns.GetHostAddresses(HostName))
{
//Console.WriteLine(ip.ToString());
try
{
Ping ping=new Ping();
ping.PingCompleted += new PingCompletedEventHandler(PingCallBackEvent);
ping.SendAsync(ip,10000);
}catch(Exception ex){
MessageBox.Show(ex.ToString());
}
}
}
public static void PingCallBackEvent(object sender,PingCompletedEventArgs e)
{
if (e.Error != null)
{
Console.WriteLine ("Ping failed:");
Console.WriteLine (e.Error.ToString ()); // Let the main thread resume.
((AutoResetEvent)e.UserState).Set();
} if(e.Reply.Status==IPStatus.Success)
{
Console.WriteLine("连通的IP:"+e.Reply.Address.ToString());
}
//((AutoResetEvent)e.UserState).Set();
}
}
测试IP是否连通是经常报错,测试也经常不通
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货