C # Implement a network segment scan
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.
table of Contents
1. Use the class
2. Get local host IP addresses
3. Remote query
4. Implement the scan of a network segment
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
// Response to the event of the Button control
Private void buttion1_click (Object sender, System.Event.Args E)
{
IPHOSTENTRY MyHost = New iphostentry ();
Try
{
// 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); } } // private 3. Remote query Private void buttion2_click (Object Sender, System.Eventargs E) { IPHOSTENTRY MYDNSTOIP = New iphostentry (); //Dns.resolve method: Eliminate the DNS hostname or dotted four-part representation // IP address to an IPHOSTENTRY instance MYDNSTOIP = DNS.Resolve (TextBox2.Text.toString ()); / / Display a list of IP addresses of this domain name For (int i = 0; i { Rich.TextBox1.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) { // Thread class: Create and control thread Thread THSCAN = New Thread (New Threadstrart (Scantarget); //Thread.start method: start thread THSCAN.STRART (); } 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 "." " ". "; // 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.GETHOSTBYADDRESS (MYSCANIP); / / Get the name of the host String strhostname = myscanhost.hostname.tostring (); RichtextBox1.AppendText (strscanipadd "->" strhostname "/ r"); } Catch (Exception Error) { Messagebox.show (Error.Message); } } // for } // private As soon as this is a simple function of the scanner, you can see the host on your network, you can see the host :), Author: Deciduous summer day