Remote monitoring-(LAN version) 1.c# network segment Scanning

Source: Internet
Author: User

Recently, the company has been developing remote video and remote monitoring projects. We just have time to summarize them,

I don't know much about it, just follow the feeling;

Query a lot of information, all scattered, some good information on foreign websites,

Many of them are implemented using C and C ++. I have limited technical skills and can only use C !!

 

In the early stage of winform testing, it was just a simple step and a simple test. After the core functions were initially passed, data transmission was refined and the UI was integrated;

 

 

1. C # implement CIDR Block scanning:

(1). Dos Method

 

 

// Ping the host of the network segment cyclically.

// Use the following functions to obtain the list of IP addresses with responses in the LAN.

Public static arraylist getalllocalmachines ()

{

PROCESS p = new process ();

P. startinfo. filename = "cmd.exe ";

P. startinfo. useshellexecute = false;

P. startinfo. redirectstandardinput = true;

P. startinfo. redirectstandardoutput = true;

P. startinfo. redirectstandarderror = true;

P. startinfo. createnowindow = true;

P. Start (); p. standardinput. writeline ("ARP-");

P. standardinput. writeline ("exit ");

Arraylist list = new arraylist ();

Streamreader reader = P. standardoutput;

String iphead = DNS. gethostbyname (DNS. gethostname (). Addresslist [0]. tostring (). substring (0, 3 );

For (string line = reader. Readline (); line! = NULL; line = reader. Readline ())

{

Line = line. Trim (); If (line. startswith (iphead) & (line. indexof ("dynamic ")! =-1 ))

{

String IP = line. substring (0, 15 ). trim (); string MAC = line. substring (line. indexof ("-")-2, 0x11 ). trim ();

Localmachine = new localmachine ();

Localmachine. machineip = IP;

Localmachine. machinemac = MAC;

Localmachine. machinename = "";

List. Add (localmachine );

}

}

Return list;

}

 

 

(2) system. Net Method

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 multiline Program Programming.
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 [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
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 "+ strhostname +" \ r ");
}
Catch (Exception error)
{
MessageBox. Show (error. Message );
}
} //
} // Private

 

 

 

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.