写了个扫描局域网端口和电脑的软件,但觉得2个网段就不好访问,比如192.168.0.1和192.168.1.1的网段。
查IP地址是可以转换为long的,IP地址起始转换为数字范围就好扫描的,但IP地址却是小头存储,总之,靠IP值好不好弄。
想到的方法是将IP的byte值转换为顺序的int值,就可以范围之间访问了。
记性差,直接贴代码,以后方便找。
//一般windows系统IsLittleEndian=true,即小头模式
public byte[] IntToByteArr(UInt32 intValue)
{
byte[] intBytes = BitConverter.GetBytes(intValue);
if (BitConverter.IsLittleEndian) Array.Reverse(intBytes);
return intBytes;
}
调用:
int startIP = ByteArrToInt(System.Net.IPAddress.Parse(tsTB_IPStart.Text).GetAddressBytes());
int endIP = ByteArrToInt(System.Net.IPAddress.Parse(tsTB_IPEnd.Text).GetAddressBytes());
for (int ip = startIP; ip <= endIP; ip++)
{
byte[] hostBytes = IntToByteArr((UInt32)ip);
System.Net.IPAddress _Address = new System.Net.IPAddress(hostBytes);
string host = _Address.ToString();
Thread thread = new Thread(() => Scan_port(host, port));
}
这样就可以跨网段扫描地址的了,例如:192.168.0.1~192.168.2.255(会扫描192.168.2.0这个地址)。
但是,扫描多个网段,则主机需要添加多个网段IP地址,才能扫描多网段的。发现的一个工具可以一个ip地址扫描多个网段,真搞不懂它是如何实现的,这个软件的名字叫:Advanced_IP_Scanner。