A typicalTCPPort ScannerConnectThe function tries to connect the server to determine whether the port on the server is open. This scanner is multi-threaded.WinSockMost programming uses multithreading technology to make full use of bandwidth, suchNetAntsOf5Ant downloads, someFTPMulti-thread upload of software, etc!
// Source code in C ++ builder5
CPP Code
- # Pragma hdrstop
- # Include "unit1.h"
- # Define threadnum 10 // Number of threads
- # Define mutexname "Welcome to lovebcb.yeah.net"
- # Pragma package (smart_init)
- # Pragma resource "*. DFM"
- Typedef StructG_scan // This is a custom Structure
- {
- CharSzfile [40]; // name of the file used to store the result
- CharSzmutex [40]; // used to store the name of the mutex. This is a method for multi-thread security.
- UnsignedShortSport; // The starting port for scanning, In the byte sequence of the Local Machine
- UnsignedShortEport; // The end port of the scan, In the byte sequence of the Local Machine
- UnsignedLongGoali; // ip address of the target host, in bytes
- IntResult; // used to store results
- } * Pg_scan;
- Tforlover * forlover; // This is the form
- HandleHthread [threadnum];
- G_scan gscan [threadnum];
- DWORDDwthreadid, dwthreadcode;
- UnsignedShortUspart; // used to separate the number of ports to be scanned and allocate them to each thread
- UnsignedLongUlip;
- IntIlivethread; // Number of threads used to store Activity
- UnsignedLongServerip (Char* Serverip );
- DWORDWinapi scanport (LpvoidLP)
- /* This is the main thread function scanport */
- DWORDWinapi scanport (LpvoidLP)
- {
- Pg_scan pgscan = (pg_scan) LP;
- CharSzresult [40];
- Sockaddr_in sock;
- UnsignedShortNowport = pgscan → sport-1; // The port number used to store the current scan.
- File* FP; // file pointer
- HandleHmutex = openmutex (mutex_all_access,False, Pgscan → szmutex );
- Socket Sk = socket (af_inet, sock_stream, 0 );
- Sock. sin_family = af_inet;
- Sock. sin_addr.s_addr = pgscan → goalip;
- While(Nowport
- {
- Sock. sin_port = htons (++ nowport)
- If(Connect (SK, (sockaddr *) & sock,Sizeof(Sock) = socket_error)
- Continue;
- /* Because the socket in blocking mode is used here, returning socket_error usually means that the connection fails. Therefore, use continue to end the loop, that is, start a loop again. If the returned value is not socket_error, the connection is successful, that is, the port is opened on the target host */
- Wsprintf (szresult, "Target Host: % sport: % d Open/R/N", inet_ntoa (sock. sin_addr), nowport); waitforsingleobject (hmutex, infinite );
- /* Use waitforsinleobject to ensure thread security. Infinite indicates waiting until the mutex has a signal */
- Fp = fopen (pgscan → szfile, "");
- Fwrite (szresult,Sizeof(Char), Strlen (szresult), FP );
- Fclose (FP );
- Pgscan → result ++;
- Releasemutex (hmutex); // release the mutex
- Closesocket (SK); // close the connection because a connection has been established.
- SK = socket (af_inet, sock_stream, 0); // recreate a socket
- }