C # single-thread, multi-thread, thread pool program design scan network IP

Source: Internet
Author: User

Create a Windows application and use multiple threads to scan a computer in a CIDR block. Obtain the DNS domain name based on the IP address of the computer. If the computer is not online, a prompt is returned. The initial interface is required.


After you enter the IP address range, click scan to automatically display the DNS information of each IP address in listboxstatus. The specific requirements are as follows:

(1) Verify the selected IP address range. If it is not a valid IP address, a prompt is displayed.

(2) create a thread to scan an IP address when performing the scan operation.

(3) Add the DNS information corresponding to each IP address to listboxstatus. For example, the "ip address-DNS domain name" format is used.

Lab requirements

(1) ipcount must be used to indicate the number of IP addresses;

(2) start must be used to indicate the starting IP address, and end must be the ending IP address.

5. Experiment steps

(1) run Microsoft Visual Studio 2008 to create a Windows application project named scancomputer.

(2) In Solution Explorer, the design interface is shown in 1-1.

(3) switch to the code mode and define a delegate under the namespace scancomputer file of form1.cs.

Public Delegate void getcomputerdnsdelegate (string strip, string strhostname );

Strip indicates the IP address, and strhostname indicates the DNS name corresponding to the IP address.

(4) define and implement the addstatusinfotolistbox method in form1.cs to add scan information to listboxstatus.

Method: Public void addstatusinfotolistbox (string strip, string strhostname)

(5) to pass multiple parameters to the thread, define a scan class under namespace scancomputer to save parameters and provide methods to convert IP addresses to DNS. The checkcomputer method converts IP addresses to DNS addresses.

(6) In the Click Event of the scan button, first determine whether the IP address range meets the requirements, then count the number of IP addresses to be calculated, and create multiple threads to perform the scan operation.

Program running diagram:

(1) single thread

 

(2) Multithreading

 
(3) thread pool

 


Source program:

Using system; using system. collections. generic; using system. LINQ; using system. text; using system. net; using system. windows. forms; namespace scancomputer {class scan {Public String strip; Public scancomputer. form1.getcomputerdnsdelegate D; Public String hostname; Public void checkcomputer () {IPaddress IP; iphostentry IPE; try {IP = IPaddress. parse (this. strip); IPE = DNS. gethostentry (IP); hostname = IPE. hostname;} catch {hostname = "no response! ";} If (D! = NULL) {d (strip, hostname) ;}} public void checkcomputerstr (Object stateinfo) {This. strip = (string) stateinfo; checkcomputer () ;}} form1.csnamespace scancomputer {public partial class form1: FORM {public form1 () {initializecomponent ();} public int ipcount; public int start; Public int end; Public String subip; Public int hasendcount; // used to determine whether all threads have ended Public Delegate void getcomputerdnsdelegat E (string strip, string strhostname); Public void addstatusinfotolistbox (string strip, string strhostname) {If (this. listboxstatus. invokerequired) {getcomputerdnsdelegate d = addstatusinfotolistbox; listboxstatus. invoke (D, strip, strhostname);} else {hasendcount ++; listboxstatus. items. add (strip + "--- domain name:" + strhostname); If (hasendcount = ipcount) {endtime = datetime. now; timespan TM = endtime- Starttime; listboxstatus. items. add ("----------------------"); listboxstatus. items. add ("when scanning is completed:" + TM. milliseconds + "millisecond"); this. buttonscansingle. enabled = true; this. buttonscanagain. enabled = true; this. buttonscanpool. enabled = true ;}} datetime starttime, endtime; IPaddress IP; iphostentry IPE; string strip, hostname; /// <summary> /// multi-thread scan /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void buttonscanagain_click (Object sender, eventargs e) {Init (); // initialization operation if (ipcount <0) {MessageBox. show ("the start address and end address are incorrect. Please check and scan again. "," Prompt ", messageboxbuttons. OK, messageboxicon. question); return;} thread [] scanthreads = new thread [ipcount]; // defines scan scanobj for a scan instance; For (INT I = 0; I <ipcount; I ++) {scanobj = new scan (); // pass the IP address scanobj. strip = subip + (numericupdownipstart. value + I ). tostring (); scanobj. D = addstatusinfotolistbox; // initialize the thread instance scanthreads [I] = new thread (scanobj. checkcomputer); // start the scanthreads [I]. start (); }}/// <Summary> /// single thread // </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void buttonscansingle_click (Object sender, eventargs e) {// initialize the IP address range Init (); If (ipcount <0) {MessageBox. show ("the start address and end address are incorrect. Please check and scan again. "," Prompt ", messageboxbuttons. OK, messageboxicon. question); return;} thread = new thread (singlethread); thread. start () ;} private void singlethread () {for (INT I = 0; I <ipcount; I ++) {strip = subip + (numericupdownipstart. value + I ). tostring (); try {IP = IPaddress. parse (strip); IPE = DNS. gethostentry (IP); hostname = IPE. hostname; addstatusinfotolistbox (strip, hostname);} catch {addstatu Sinfotolistbox (strip, "No response! ");}}} /// <Summary> /// thread pool scan /// </Summary> /// <Param name = "sender"> </param> /// <Param name = "E"> </param> private void buttonscanpool_click (Object sender, eventargs e) {Init (); scan scanobj; For (INT I = 0; I <ipcount; I ++) {scanobj = new scan (); // pass the IP address scanobj. strip = subip + (numericupdownipstart. value + I ). tostring (); scanobj. D = addstatusinfotolistbox; // Add the task to the thread queue threadpool. queueuserworkitem (scanobj. checkcomputerstr, scanobj. strip) ;}/// <summary> // ip address Range Calculation /// </Summary> private void Init () {starttime = datetime. now; this. listboxstatus. items. clear (); this. buttonscanagain. enabled = false; this. buttonscansingle. enabled = false; this. buttonscanpool. enabled = false; subip = numericupdownip1.value + ". "+ numericupdownip2.value + ". "+ numericupdownip3.value + ". "; Start = (INT) numericupdownipstart. value; end = (INT) numericupdownipend. value; ipcount = end-start + 1; hasendcount = 0 ;}}}

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.