Socket (3)

Source: Internet
Author: User

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
  1. # Pragma hdrstop
  2. # Include "unit1.h"
  3. # Define threadnum 10 // Number of threads
  4. # Define mutexname "Welcome to lovebcb.yeah.net"
  5. # Pragma package (smart_init)
  6. # Pragma resource "*. DFM"
  7. Typedef StructG_scan // This is a custom Structure
  8. {
  9. CharSzfile [40]; // name of the file used to store the result
  10. CharSzmutex [40]; // used to store the name of the mutex. This is a method for multi-thread security.
  11. UnsignedShortSport; // The starting port for scanning, In the byte sequence of the Local Machine
  12. UnsignedShortEport; // The end port of the scan, In the byte sequence of the Local Machine
  13. UnsignedLongGoali; // ip address of the target host, in bytes
  14. IntResult; // used to store results
  15. } * Pg_scan;
  16. Tforlover * forlover; // This is the form
  17. HandleHthread [threadnum];
  18. G_scan gscan [threadnum];
  19. DWORDDwthreadid, dwthreadcode;
  20. UnsignedShortUspart; // used to separate the number of ports to be scanned and allocate them to each thread
  21. UnsignedLongUlip;
  22. IntIlivethread; // Number of threads used to store Activity
  23. UnsignedLongServerip (Char* Serverip );
  24. DWORDWinapi scanport (LpvoidLP)
  25. /* This is the main thread function scanport */
  26. DWORDWinapi scanport (LpvoidLP)
  27. {
  28. Pg_scan pgscan = (pg_scan) LP;
  29. CharSzresult [40];
  30. Sockaddr_in sock;
  31. UnsignedShortNowport = pgscan → sport-1; // The port number used to store the current scan.
  32. File* FP; // file pointer
  33. HandleHmutex = openmutex (mutex_all_access,False, Pgscan → szmutex );
  34. Socket Sk = socket (af_inet, sock_stream, 0 );
  35. Sock. sin_family = af_inet;
  36. Sock. sin_addr.s_addr = pgscan → goalip;
  37. While(Nowport
  38. {
  39. Sock. sin_port = htons (++ nowport)
  40. If(Connect (SK, (sockaddr *) & sock,Sizeof(Sock) = socket_error)
  41. Continue;
  42. /* 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 */
  43. Wsprintf (szresult, "Target Host: % sport: % d Open/R/N", inet_ntoa (sock. sin_addr), nowport); waitforsingleobject (hmutex, infinite );
  44. /* Use waitforsinleobject to ensure thread security. Infinite indicates waiting until the mutex has a signal */
  45. Fp = fopen (pgscan → szfile, "");
  46. Fwrite (szresult,Sizeof(Char), Strlen (szresult), FP );
  47. Fclose (FP );
  48. Pgscan → result ++;
  49. Releasemutex (hmutex); // release the mutex
  50. Closesocket (SK); // close the connection because a connection has been established.
  51. SK = socket (af_inet, sock_stream, 0); // recreate a socket
  52. }

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.