C # Build a fast Port Scanner

Source: Internet
Author: User

Implement Windows Forms)

Implement console version)

It's easy to use another port scanner, but if you can do one, it's of course a pleasant thing. In fact, it is not difficult to implement your own port scanner. The principle is also relatively simple, as long as we have basicNetworkKnowledge, understand the TCP three-way handshake principle.
C #Provides a set of network programming classes to make network programming simpler and more elegant. Among them, socket encapsulates most of the APIs used for network communication. To implement port scanning, we only need to use the socket. Connect () method. Then determine whether socket. Connected is true. If it is true, it indicates that the port can be connected, that is, the port is open. Therefore, determine whether a port is openCodeAs follows:

 1   Public   Void Threadfunc ( Object O)
2 {
3 Int Port = ( Int ) O;
4 Scansocket S = New Scansocket (addressfamily. InterNetwork, sockettype. Stream, protocoltype. TCP );
5 S. Port = port;
6 Ipendpoint ipport = New Ipendpoint (scanip, Port );
7 Try
8 {
9 // S. beginconnect (ipport, new asynccallback (endconnect), S );
10 S. Connect (ipport );
11 If (S. Connected = True )
12 Onportconnect ( Null , New Scaneventarg (S. Port ));
13 }
14 Catch (Exception E)
15 {
16
17 }
18 Finally
19 {
20 Lock (This )
21 {
22 Finishthreads ++;
23 }
24 If (Onportthreadfinish! = Null )
25 Onportthreadfinish ( Null , Null );
26 }
27 }

To achieve fast scanning, it is inevitable to use multiple threads. My idea is that each thread only scans one port number. After scanning, assign a new port number. The Code is as follows:

 1   Private   Void Mainscan ()
2 {
3 If (! Adjustportandip (IP, Ref Sport, Ref Eport ))
4 {
5 If (Onporterror! = Null )
6 Onporterror ( Null , Null );
7 Return ;
8 }
9 If (Maxthreadcount> default_thread_count)
10 {
11 Maxthreadcount = default_thread_count;
12 }
13 Realthreadcount = 0 ;
14
15 Scanthread ST = New Scanthread ();
16 St. scanip = This . Scanip;
17 St. onportconnect + =New Scanhandler (st_onportconnect );
18 St. onportthreadfinish + = New Eventhandler (st_onportthreadfinish );
19 For ( Int I = sport; I <= eport; I ++, realthreadcount ++)
20 {
21 While RealThreadCount-st.FinishThreads> maxthreadcount)
22 {
23 Thread. Sleep ( 10 );
24 }
25 Thread. Sleep ( 10 );
26 Parameterizedthreadstart PS = New Parameterizedthreadstart (St. threadfunc );
27 Thread start = New Thread (PS );
28 Start. Start (I );
29 }
30 While (St. finishthreads! = Realthreadcount) thread. Sleep ( 1000 ); // Wait until all threads exit
31 // Thread. Sleep (10000 );
32 If (Onscanfinish! = Null ) // Notification Client Scan completed
33 Onscanfinish ( Null , Null );
34 }

To interact with customer segments, several events are defined in scanport. CS:

1 Public EventEventhandler onporterror;
2Public EventEventhandler onscanfinish;
3Public EventScanhandler onportconnect;
4Public EventEventhandler onportthreadfinish;

The scan is completed. The port is opened and the scan is completed. Well, with these foundations, it is not difficult to implement your own scanner. For specific implementation, refer to the original code.
How to Use: First add scandll. DLL to the project, and then specify the IP address and port number. The Code is as follows:
Scanport. Scan (IP, sport, eport, threadcount );

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.