// Reference Space
Using system. net;
Using system. Threading;
Private void button#click (Object sender, system. eventargs E)
{
// Thread class: Creates and controls threads.
Thread thscan = new thread (New threadstart (scantarget ));
// Thread. Start method: Start the thread
Thscan. Start ();
}
Private void scantarget ()
{
String strstartipaddress = This. textbox1.text + "." + textbox2.text + "." + textbox3.text + "." + textbox4.text;
String strendipaddress = This. textbox5.text + "." + textbox6.text + "." + textbox7.text + "." + textbox8.text;
If (isvalidip (strstartipaddress) & isvalidip (strendipaddress ))
{
Int intstartipaddress = convertipaddresstonumber (strstartipaddress );
Int intendipaddress = convertipaddresstonumber (strendipaddress );
// Total number of scanned IP addresses
Int totalscannumber = intendipaddress-intstartipaddress;
// Scan operation
For (INT I = 0; I <= totalscannumber; I ++)
{
Int intcurrentscanip = I + intstartipaddress;
String strcurrentscanip = convertnumbertoipaddress (intcurrentscanip );
// Convert to IP Address
IPaddress myscanip = IPaddress. parse (strcurrentscanip );
Try
{
// DNS. gethostbyaddress method: Obtain DNS host information based on the IP address.
Iphostentry myscanhost = DNS. gethostbyaddress (myscanip );
// Obtain the Host Name
String strhostname = myscanhost. hostname. tostring ();
Richtextbox1.appendtext ("Scan IP:" + strcurrentscanip + "corresponding host:" + strhostname + "/R ");
}
Catch (Exception error)
{
Richtextbox1.appendtext ("Scan IP:" + strcurrentscanip + "failed:" + error. Message + "/R ");
}
}
}
Else
{
Richtextbox1.appendtext ("Scan failed: the Entered IP address is invalid" + "/R ");
}
}
Private int countipnumber (string startipaddress, string endipaddress)
{
// Determine the number of IP addresses between two IP addresses
If (isvalidip (startipaddress) & isvalidip (endipaddress ))
{
// Convert the IP address to a number for comparison
String [] maxip_ary = endipaddress. Split ('.');
Int tmp_maxip = convert. toint32 (maxip_ary [0]) * 256*256*256 + convert. toint32 (maxip_ary [1]) * 256*256 + convert. toint32 (maxip_ary [2]) * 256 + convert. toint32 (maxip_ary [3]);
String [] minip_ary = startipaddress. Split ('.');
Int tmp_minip = convert. toint32 (minip_ary [0]) * 256*256*256 + convert. toint32 (minip_ary [1]) * 256*256 + convert. toint32 (minip_ary [2]) * 256 + convert. toint32 (minip_ary [3]);
Return tmp_maxip-tmp_minip;
}
Else
{
Return 0;
}
}
Private int convertipaddresstonumber (string stripaddress)
{
// Convert the target IP address string stripaddress to a number
String [] arrayip = stripaddress. Split ('.');
Int SIP1 = int32.parse (arrayip [0]);
Int sip2 = int32.parse (arrayip [1]);
Int sip3 = int32.parse (arrayip [2]);
Int sip4 = int32.parse (arrayip [3]);
Int tmpipnumber;
Tmpipnumber = SIP1 * 256*256*256 + sip2 * 256*256 + sip3 * 256 + sip4;
Return tmpipnumber; // return the number after the IP address is converted.
}
Private string convertnumbertoipaddress (INT intipaddress)
{
// Convert the target integer intipaddress to an IP address string
//-1062731518 192.168.1.2
//-1062731517 192.168.1.3
Int tempipaddress = intipaddress + 1;
Int S1 = tempipaddress/256/256/256;
Int S21 = S1 x 256x256x256;
Int S2 = (tempIPAddress-s21)/256/256;
Int s31 = S2 x 256*256 + S21;
Int S3 = (tempIPAddress-s31)/256;
Int S4 = tempIPAddress-s3 * 256-s31;
If (S1 <0)
{
S1 = 255 + S1;
S2 = 255 + S2;
S3 = 255 + S3;
S4 = 255 + S4;
}
String stripaddress = s1.tostring () + "." + s2.tostring () + "." + s3.tostring () + "." + s4.tostring ();
Return stripaddress; // return the IP address string.
}
// The following function is incorrect. For example, 192.168.1.249 gets a negative number.
Private bool isvalidip (string stripaddress)
{
// The IP address to be verified: stripaddress
String ipmax = "255.255.255.255"; // maximum allowed IP address range
String ipmin = "0.0.0.0"; // minimum allowed IP address range
// Convert the IP address to a number for comparison
String [] arrayip = stripaddress. Split ('.');
Int tmp_userip = convert. toint32 (arrayip [0]) * 256*256*256 + convert. toint32 (arrayip [1]) * 256*256 + convert. toint32 (arrayip [2]) * 256 + convert. toint32 (arrayip [3]);
String [] maxip_ary = ipmax. Split ('.');
Int tmp_maxip = convert. toint32 (maxip_ary [0]) * 256*256*256 + convert. toint32 (maxip_ary [1]) * 256*256 + convert. toint32 (maxip_ary [2]) * 256 + convert. toint32 (maxip_ary [3]);
String [] minip_ary = ipmin. Split ('.');
Int tmp_minip = convert. toint32 (minip_ary [0]) * 256*256*256 + convert. toint32 (minip_ary [1]) * 256*256 + convert. toint32 (minip_ary [2]) * 256 + convert. toint32 (minip_ary [3]);
If (tmp_userip <= tmp_maxip & tmp_userip> = tmp_minip)
{
Return true; // the IP address is valid.
}
Else
{
Return false; // the IP address is invalid.
}
}
// The corrected function is
Private Static bool isvalidip (string IP)
{
String [] parts = IP. Split ('.');
If (parts. length! = 4) return false;
Try
{
If (byte. parse (parts [0]) = 0x00)
Return false;
Foreach (string part in parts)
If (byte. parse (Part)> 0xff)
Return false;
}
Catch (exception) {return false ;}
Return true;
}
// Verify the validity of port numbers
Private Static bool isvalidport (string strport)
{
// The IP address to be verified: stripaddress
Int maxport = 65535; // maximum port number allowed
Int minport = 0; // The minimum port number allowed
Try
{
Int tempport = int32.parse (strport );
If (tempport <= maxport & tempport> = minport)
{
Return true; // the IP address is valid.
}
Else
{
Return false; // the IP address is invalid.
}
}
Catch
{
Return false;
}
}