C # CIDR Block Scanning
Summary
I think everyone is very familiar with the scanners such as Xiaorong time. Do you have the impulse to write your own articles. Recently, Microsoft has implemented the push. net strategic solution. C # is the main language. Are you interested in using C # To scan lan ip addresses and try to write your own happiness? Come with me.
--------------------------------------------------------------------------------
Directory
1. Classes used
2. Obtain the IP address of the local host
3. Remote Query
4. Scan network segments
--------------------------------------------------------------------------------
Body
1. first introduce the classes used:
DNS class: In. Net's system.net namespace, the main function is to retrieve information about a specific host from the Internet Domain Name System (DNS.
Iphostentry class: associate a Domain Name System (DNS) host with a group name and a group of matched IP addresses, and use it with the DNS class.
IPaddress class: IP address on the IP network.
The namespaces used include:
The system. Net namespace provides simple programming interfaces for various protocols used on the current network.
The system. Io namespace contains the types that allow synchronous and asynchronous reading and writing on data streams and files.
The system. Thread namespace is mainly used for multilineProgramProgramming.
The program implements the following functions:
2. Obtain the IP address of the local host
// Response to the button control event
Private void buttionmediaclick (Object sender, system. event. ARGs E)
{
Iphostentry myhost = new iphostentry ();
Try
{
// DNS. gethostname () Get the Host Name of the Local Computer
// DNS. gethostbyname () obtains the DNS information of the specified DNS host name
// Obtain the DNS information of the local host
Myhost = DNS. gethostbyname (DNS. gethostname ());
// Display the local host name
Textbox1.text = myhost. hostname. tostring ();
// Display the IP address table of the local host
For (INT I = 0; I <myhost. Addresslist. length; I ++)
{
Richtextbox1.appendtext ("local host IP address->" + myhost. Addresslist [I]. tostring () + "\ r ");
}
Catch (Exception error)
{
MessageBox. Show (error. Message );
}
} // Private
3. Remote Query
Private void buttion2_click (Object sender, system. eventargs E)
{
Iphostentry mydnstoip = new iphostentry ();
// DNS. Resolve Method: Resolve the DNS host name or the IP address in the four-part notation separated by dots to an iphostentry instance
Mydnstoip = DNS. Resolve (textbox2.text. tostring ());
// Display the list of IP addresses for this domain name
For (INT I = 0; I <mydnstoipaddresslist. length; I ++)
{
Rich. textbox1.appendtext (the IP address of textbox2.text + is "+ mydnstoip. Addresslist [I]. tostring () +" \ r ");
}
}
4. Scan network segments
Scans network segments to determine the number of hosts in use in the network. The multithreading technology is used here and a thread is added. To prevent the program from being scanned for too long and affect the program response. However, due to the use of the garbage collection technology in. net, thread control is not very complex.
Private void button3_click (Object sender, system. eventargs E)
{
// Thread class: Creates and controls threads.
Thread thscan = new thread (New threadstrart (scantarget ));
// Thread. Start method: Start the thread
Thscan. strart ();
}
Private void scantarget ()
{
// Construct the 31-8bit of the IP address, that is, the front end of the fixed IP Address
// Numericupdown1 is the defined system. Windows. Forms. numericupdown control.
String stripaddress = numericupdown1.text + "." + numericupdown2.text + "." + numericupdown3.text + ".";
// Start scanning address
Int nstrat = int32.parse (numericupdown4.text );
// Terminate the scan address
Int nend = int32.parse (numericupdown5.text );
// Scan operation
For (INT I = nstrat; I <= nend; I ++)
{
String strscanipadd = stripaddress + I. tostring ();
// Convert to IP Address
IPaddress myscanip = IPaddress. parse (strscanipadd );
Try
{
// You can add your own and enhance Functions
// DNS. gethostbyaddress method: Based on the IP address
// Obtain DNS host information.
Iphostentry myscanhost =
DNS. gethostbyaddress (myscanip );
// Obtain the Host Name
String strhostname = myscanhost. hostname. tostring ();
Richtextbox1.appendtext (strscanipadd + "->" + strhostname + "\ r ");
}
Catch (Exception error)
{
MessageBox. Show (error. Message );
}
} //
} // Private
So far, a simple use of C # to implement the main functions of the scanner is complete, try it, you can see the host on your network, have a sense of accomplishment :)
++
I think everyone is very familiar with the scanners such as Xiaorong time. Do you have the impulse to write your own articles. Recently, Microsoft has implemented the push. net strategic solution. C # is the main language. Are you interested in using C # To scan lan ip addresses and try to write your own happiness? Come with me.
First, we will introduce the classes used:
1. DNS class: In the. NET system.net namespace, the main function is to retrieve information about a specific host from the Internet Domain Name System (DNS.
2. iphostentry class: associate a Domain Name System (DNS) host with a group name and a group of matched IP addresses for use with the DNS class.
3. IPaddress class: IP address on the IP network.
The namespaces used include:
The system. Net namespace provides simple programming interfaces for various protocols used on the current network.
The system. Io namespace contains the types that allow synchronous and asynchronous reading and writing on data streams and files.
The system. Thread namespace is mainly used for multi-thread sequential programming.
The program implements the following functions:
1. Obtain the IP address of the local host
// Response to the button control event
Private void buttionmediaclick (Object sender, system. event. ARGs E)
{
Iphostentry myhost = new iphostentry ();
Try
{
// DNS. gethostname () Get the Host Name of the Local Computer
// DNS. gethostbyname () obtains the DNS information of the specified DNS host name
// Obtain the DNS information of the local host
Myhost = DNS. gethostbyname (DNS. gethostname ());
// Display the local host name
Textbox1.text = myhost. hostname. tostring ();
// Display the IP address table of the local host
For (INT I = 0; I <myhost. Addresslist. length; I ++)
{
Richtextbox1.appendtext ("local host IP address à" + myhost. Addresslist [I]. tostring () + "\ r ");
}
Catch (Exception error)
{
MessageBox. Show (error. Message );
}
} // Private
2. Remote Query
Private void buttion2_click (Object sender, system. eventargs E)
{
Iphostentry mydnstoip = new iphostentry ();
// DNS. Resolve Method: Resolve the DNS host name or the IP address in the four-part notation separated by dots to an iphostentry instance
Mydnstoip = DNS. Resolve (textbox2.text. tostring ());
// Display the list of IP addresses for this domain name
For (INT I = 0; I <mydnstoipaddresslist. length; I ++)
{
Rich. textbox1.appendtext (the IP address of textbox2.text + is "+ mydnstoip. Addresslist [I]. tostring () +" \ r ");
}
}
3. Scan the network segment to determine the number of hosts in use in the network. The multithreading technology is used here and a thread is added. To prevent the program from being scanned for too long and affect the program response. However, due to the use of the garbage collection technology in. net, thread control is not very complex.
Private void button3_click (Object sender, system. eventargs E)
{
// Thread class: Creates and controls threads.
Thread thscan = new thread (New threadstrart (scantarget ));
// Thread. Start method: Start the thread
Thscan. strart ();
}
Private void scantarget ()
{
// Construct the 31-8bit of the IP address, that is, the front end of the fixed IP Address
// Numericupdown1 is the defined system. Windows. Forms. numericupdown control.
String stripaddress = numericupdown1.text + "." + numericupdown2.text + "." + numericupdown3.text + ".";
// Start scanning address
Int nstrat = int32.parse (numericupdown4.text );
// Terminate the scan address
Int nend = int32.parse (numericupdown5.text );
// Scan operation
For (INT I = nstrat; I <= nend; I ++)
{
String strscanipadd = stripaddress + I. tostring ();
// Convert to IP Address
IPaddress myscanip = IPaddress. parse (strscanipadd );
Try
{
// You can add your own and enhance Functions
// DNS. gethostbyaddress method: Based on the IP address
// Obtain DNS host information.
Iphostentry myscanhost =
DNS. gethostbyaddress (myscanip );
// Obtain the Host Name
String strhostname = myscanhost. hostname. tostring ();
Richtextbox1.appendtext (strscanipadd + "à" + strhostname + "\ r ");
}
Catch (Exception error)
{
MessageBox. Show (error. Message );
}
} //
} // Private