Code _c# tutorial for implementing network Segment scanning in C #

Source: Internet
Author: User
Tags garbage collection thread class
Summary
I guess everyone is very familiar with the scanners such as the time of Banyan, there is no impulse to write one's own. Recently Microsoft pushed it into practice. NET strategy, C # is the main push language, you are interested in using C # to achieve the LAN IP address scan, try to write their own happiness, then please follow me.
Body
1. First, introduce the class used:
DNS class: Under the System.Net namespace in. NET, the primary function is to retrieve information about a specific host from the Internet Domain Name System (DNS).
Iphostentry class: Associates a Domain Name System (DNS) host with a set of aliases and a matching set of IP addresses, and is used in conjunction with the DNS class.
IPAddress class: Addresses on an IP network.
The namespaces used are:
The System.Net namespace provides a simple programming interface for a variety of protocols that are used on the current network.
The System.IO namespace contains types that allow synchronous and asynchronous reads and writes on data streams and files.
System.thread namespaces are used primarily for multi-line programming.
The program implements the following functions:
2. Get the local host IP address
<summary>
Press the button to query native IP
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button1_Click (object sender, System.EventArgs e)
{
Iphostentry myHost = new Iphostentry ();
Try
{
This.richTextBox1.Text = "";
Dns.gethostname () Gets the host name of the local computer
Dns.gethostbyname () Gets DNS information for the specified DNS host name
Get DNS information for the local host
MyHost = Dns.gethostbyname (Dns.gethostname ());
Show Local Host name
TextBox1.Text = MyHost.HostName.ToString ();
Displays the IP Address table for 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);
}
}
3. Remote Query
private void Button2_Click (object sender, System.EventArgs e)
{
This.richTextBox1.Text = "";
Iphostentry Mydnstoip = new Iphostentry ();
Dns.resolve method: The DNS host name or the dot-delimited four-part notation format
IP address resolution to iphostentry instance
Mydnstoip =dns.resolve (textBox2.Text.ToString ());
Displays a list of IP addresses for this domain name
for (int i=0;i<mydnstoip.addresslist.length;i++)
{
Richtextbox1.appendtext (TextBox2.Text + "IP address is" + mydnstoip.addresslist[i]. ToString () + "\ r");
}
}
4. Realize the network segment scanning
Implement network segment scanning to determine the number of hosts being used in the network. This use of multithreading technology, adding a thread, in order to prevent the program scan time is too long, affecting the program's response. However, the control of threads is not complicated in. NET because of the use of garbage collection techniques.
private void Button3_Click (object sender, System.EventArgs e)
{
This.richTextBox1.Text = "";
Thread class: Creating and controlling threads
Thread Thscan = new Thread (new ThreadStart (Scantarget));
Thread Thscan = new Thread (new ThreadStart (Scantarget));
Thread.Start method: Start a thread
Thscan.start ();
}
private void Scantarget ()
{
Constructs the 31-8bit bit of an IP address, which is the previous segment of a fixed IP address
NUMERICUPDOWN1 is the System.Windows.Forms.NumericUpDown control that is defined
String stripaddress = Numericupdown1.text + "." + Numericupdown2.text + "." + Numericupdown3.text + ".";
Start scanning addresses
int nstrat = Int32.Parse (Numericupdown4.text);
Terminate scan Address
int nend =int32.parse (numericupdown5.text);
Scan actions
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, enhanced features
Dns.gethostbyaddress method: According to IP land
Address to obtain 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);
}
}
}
That's it. A simple use of C # to implement the scanner's main function is complete, try you can see the host on your network, there is no sense of accomplishment:
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.