如何扫描IP范围C#
如何扫描IP的特定范围并将其增加到用户定义的范围。。就像大多数端口扫描仪一样。但是如何增加主机位。。它增加了网络位如何扫描IP范围C#,c#,networking,ip,range,network-scan,C#,Networking,Ip,Range,Network Scan,如何扫描IP的特定范围并将其增加到用户定义的范围。。就像大多数端口扫描仪一样。但是如何增加主机位。。它增加了网络位 private static void sendAsyncPingPacket(string hostToPing) { try { int timeout = 5000; AutoResetEvent waiter = new AutoResetEvent(false);
private static void sendAsyncPingPacket(string hostToPing)
{
try
{
int timeout = 5000;
AutoResetEvent waiter = new AutoResetEvent(false);
Ping pingPacket = new Ping();
//ping completion event reaised
pingPacket.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
string data = "Ping test check";
byte[] byteBuffer = Encoding.ASCII.GetBytes(data);
PingOptions pingOptions = new PingOptions(64, true);
Console.WriteLine("Time to live: {0}", pingOptions.Ttl);
//Console.WriteLine("Don't fragment: {0}", pingOptions.DontFragment);
pingPacket.SendAsync(hostToPing, timeout, byteBuffer, pingOptions, waiter);
//do something useful
waiter.WaitOne();
Console.WriteLine("Ping RoundTrip returned, Do something useful here...");
}
catch (PingException pe)
{
Console.WriteLine("INVALID IP ADDRESS FOUND");
}
catch (Exception ex)
{
Console.WriteLine("Exceptin " + ex.Message);
}
}
private static void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
try
{
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>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ");
//this will print exception
//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();
}
catch (PingException pe)
{
Console.WriteLine("INVALID IP ADDRESS");
}
catch (Exception ex)
{
Console.WriteLine("Exception " + ex.Message);
}
}
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);
}
}
private static long ToInt(string addr)
{
return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
(int)System.Net.IPAddress.Parse(addr).Address);
}
private static string ToAddr(long address)
{
return System.Net.IPAddress.Parse(address.ToString()).ToString();
}
static int temp = 0;
private static void scanLiveHosts(string ipFrom, string ipTo)
{
long from = Program.ToInt(ipFrom);
long to = Program.ToInt(ipTo);
long ipLong = Program.ToInt(ipFrom);
while ( from < to)
{
string address = Program.ToAddr(ipLong);
Program.sendAsyncPingPacket(address);
ipLong++;
}
}
static void Main(string[] args)
{
try
{
Program.getDeviceList();
Program.sendAsyncPingPacket("192.168.3.72");
Program.scanLiveHosts("192.168.3.1", "192.168.3.41");
}
catch (InvalidOperationException ioe)
{
Console.WriteLine(ioe.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
私有静态void sendAsyncPingPacket(字符串hostToPing)
{
尝试
{
int超时=5000;
自动重置事件服务员=新自动重置事件(假);
Ping pingPacket=新Ping();
//ping完成事件
pingPacket.PingCompleted+=新的PingCompletedEventHandler(PingCompletedCallback);
string data=“Ping测试检查”;
byte[]byteBuffer=Encoding.ASCII.GetBytes(数据);
PingOptions PingOptions=新的PingOptions(64,true);
WriteLine(“生存时间:{0}”,pingOptions.Ttl);
//WriteLine(“不分段:{0}”,pingOptions.DontFragment);
SendAsync(主机定位、超时、字节缓冲、pingOptions、服务员);
//做些有用的事
服务员,等一等;
WriteLine(“Ping往返返回,在这里做一些有用的事情…”);
}
捕获(Pingpe异常)
{
Console.WriteLine(“找到无效IP地址”);
}
捕获(例外情况除外)
{
Console.WriteLine(“例外”+ex.Message);
}
}
私有静态void PingCompletedCallback(对象发送方,PingCompletedEventArgs e)
{
尝试
{
如果(如已取消)
{
Console.WriteLine(“Ping已取消”);
//让主线程继续。
//UserToken是主线程调用的AutoResetEvent对象
//他正在等待。
((AutoResetEvent)e.UserState).Set();
}
//如果发生错误,则向用户显示异常。
如果(例如错误!=null)
{
控制台.WriteLine(“Ping失败>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>”;
//这将导致打印异常
//Console.WriteLine(e.Error.ToString());
//让主线程继续。
((AutoResetEvent)e.UserState).Set();
}
PingReply=e.回复;
显示回复(回复);
//让主线程继续。
((AutoResetEvent)e.UserState).Set();
}
捕获(Pingpe异常)
{
Console.WriteLine(“无效IP地址”);
}
捕获(例外情况除外)
{
Console.WriteLine(“异常”+ex.Message);
}
}
公共静态void DisplayReply(PingReply-reply)
{
if(reply==null)
返回;
Console.WriteLine(“ping状态:{0}”,reply.status);
if(reply.Status==IPStatus.Success)
{
Console.WriteLine(“地址:{0}”,reply.Address.ToString());
Console.WriteLine(“往返时间:{0}”,reply.RoundtripTime);
Console.WriteLine(“生存时间:{0}”,reply.Options.Ttl);
//Console.WriteLine(“不分段:{0}”,reply.Options.DontFragment);
Console.WriteLine(“缓冲区大小:{0}”,reply.Buffer.Length);
}
}
私有静态longtoint(字符串地址)
{
返回(长)(uint)System.Net.IPAddress.NetworkToHostOrder(
(int)System.Net.IPAddress.Parse(addr.Address);
}
专用静态字符串到地址(长地址)
{
返回System.Net.IPAddress.Parse(address.ToString()).ToString();
}
静态温度=0;
私有静态void scanLiveHosts(字符串ipFrom、字符串ipTo)
{
long from=程序.ToInt(ipFrom);
long to=程序.ToInt(ipTo);
long ipLong=Program.ToInt(ipFrom);
while(从<到)
{
字符串地址=Program.ToAddr(ipLong);
程序发送异步数据包(地址);
ipLong++;
}
}
静态void Main(字符串[]参数)
{
尝试
{
Program.getDeviceList();
Program.sendAsyncPingPacket(“192.168.3.72”);
scanLiveHosts(“192.168.3.1”、“192.168.3.41”);
}
捕获(无效操作异常ioe)
{
控制台写入线(ioe消息);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
}
使用的主要方法有
private static long ToInt(string addr)
{
return (long)(uint)System.Net.IPAddress.NetworkToHostOrder(
(int)System.Net.IPAddress.Parse(addr).Address);
}
private static string ToAddr(long address)
{
return System.Net.IPAddress.Parse(address.ToString()).ToString();
}
上面的文章深入介绍了如何发送ping数据包和扫描网络 嘿,我找到了谷歌的代码,我做了一些小改动来适应我的代码,也做了一些其他改动来适应你的代码。。。也许你应该考虑一下改变:
while ( from < to)
{
string address = Program.ToAddr(ipLong);
Program.sendAsyncPingPacket(address);
ipLong++;
}
while(从<到)
{
字符串地址=Program.ToAddr(ipLong);
程序发送异步数据包(地址);
ipLong++;
}
例如:
while ( ipLong < to)
{
string address = Program.ToAddr(ipLong);
Program.sendAsyncPingPacket(address);
ipLong++;
}
while(ipLong
或者IP的“探索”不会在ToAddress(to)上停止。你能展示你到目前为止的代码吗?getDeviceList()方法的定义在哪里?我现在不记得了,只是从适配器获取设备列表。