Summary
I want everyone to be very familiar with scanners such as Xiaoyu Time. Do you write an impulse yourself. Recently, Microsoft has implemented the .NET strategy plan, C # is a main language, do you have interest to use C # to achieve a scan for a local area network IP address, try to write the happiness yourself, then please come with me.
text
1. Let's introduce the class used:
DNS class: Under the system.net namespace in .NET, the main function is to retrieve information about a particular host from the Internet Domain Name System (DNS).
IPHOSTENTRY Class: Associate a Domain Name System (DNS) host with a group of aliases and a set of matching IP addresses, and uses with the DNS class.
IPaddress class: address on IP network.
The namespace used is:
System.Net Namespace provides a simple programming interface for multiple protocols used on the current network.
The System.io namespace contains types that allow synchronization and asynchronous reading and writing on data streams and files.
System.Thread namespace is mainly used to program multiple-line programs.
Program implements the following features:
2. Get local host IP addresses
///
/// Press twisting query this machine IP
/// summary>
/// param>
/// param>
Private void Button1_Click (Object Sender, System.Eventargs E)
{
IPHOSTENTRY MyHost = New iphostentry ();
Try
{
this.richtextbox1.text = //;
// DNS.GETHOSTNAME () Get the host name of the local computer
// dns.gethostByname () Get DNS information specified by the specified DNS host name
/ / Get DNS information from the local host
MyHost = DNS.GethostByname (DNS.GETHOSTNAME ());
/ / Display local hostname
TextBox1.text = myhost.hostname.tostring ();
/ / Display the IP address table of the local host
For (int i = 0; i { RichtextBox1.AppendText (/ local host IP address -> / myhost.addresslist [i] .tostring () // r /); } } Catch (Exception Error) { Messagebox.show (Error.Message); } } 3. Remote query Private void button2_click (Object Sender, System.Eventargs E) { this.richtextbox1.text = //; IPHOSTENTRY MYDNSTOIP = New iphostentry (); //Dns.resolve method: Put the DNS hostname or separately separated four partial representation format format // IP address resolution to IPHOSTENTRY instance MYDNSTOIP = DNS.Resolve (TextBox2.Text.toString ()); / / Display a list of IP addresses of this domain name For (int i = 0; i { RichtextBox1.AppendText (TextBox2.text / IP address is / mydnstoip.addresslist [i] .tostring () // r /); } 4. Implement the scan of a network segment Implement the scan of the network segment to determine the number of hosts being used in the network. Here, multi-threading technology is used, an increase of a thread, in order to prevent the program's time from being too long, affect the response of the program. However, the control of threads is not very complicated due to the use of garbage collection techniques. Private void button3_click (Object Sender, System.Eventargs E) { this.richtextbox1.text = //; // Thread class: Create and control thread // Thread THSCAN = New Thread (New ThreadStart (Scantarget); Thread THSCAN = New Thread (New ThreadStart (Scantarget)); //Thread.start method: start thread THSCAN.START (); } Private void scantarget () { / / Construct the 31-8bit bit of the IP address, that is, the front section of the fixed IP address // numericupdown1 is the defined system.Windows.Forms.NumericUpdown control String stripaddress = numericupdown1.text /. numericupdown2.text /./ numericupdown3.text /. // Start the scan 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 (strscanipad); Try { / / You can join your own, enhanced // dns.gethostbyaddress method: According to IP / Address Get DNS host information. IPHOSTENTRY MYSCANHOST = DNS.GETOSTBYADDRESS (MyScanip); / / Get the name of the host String strhostname = myscanhost.hostname.tostring (); RichtextBox1.AppendText (strscanipadd / -> / strHostname // r /); } Catch (Exception Error) { Messagebox.show (Error.Message); } } }