C ++ port scanner and Port Scanner

Source: Internet
Author: User

C ++ port scanner and Port Scanner
I. Principle

The principle of port scanning is very simple, that is, to establish socket communication and switch over the port without passing through the connect function. If it succeeds, it indicates the port developer; otherwise, the port is closed.

Familiar with all the socket programs, this content is in the window environment

Second, single-thread implementation

 

// PortScanf. cpp: defines the entry point of the console application. // # Define WIN32_LEAN_AND_MEAN # include "stdafx. h "# include <WinSock2.h> # pragma comment (lib," Ws2_32 ") int scant (char * Ip, int StartPort, int EndPort) {WSADATA wsa; SOCKET s; struct sockaddr_in server; int CurrPort; // current port int ret; WSAStartup (MAKEWORD (2, 2), & wsa); // before using the winsock function, you must use the WSAStartup function to load and initialize the dynamic Connection Library server. sin_family = AF_INET; // specify the address format. In winsock, you can only use AF_INETserver.sin_addr.s_addr = inet_addr (Ip); // specify IP address to be scanned for (CurrPort = StartPort; CurrPort <= EndPort; CurrPort ++) {s = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); server. sin_port = htons (CurrPort); // specify the port number of the IP address to be scanned. ret = connect (s, (struct sockaddr *) & server, sizeof (server )); // Connect if (0 = ret) // determine whether the connection is successful {printf ("% s: % d Success O (Success _ Success) O ~~ \ N ", Ip, CurrPort); closesocket (s);} else {printf (" % s: % d Failed \ n ", Ip, CurrPort );}} printf ("Cost time: % f second \ n", CostTime); // WSACleanup (); // release the dynamic Connection Library and release the created socket return 1;} int main () {scant ("127.0.0.1", 75,100); return 0 ;}

  

 

Third, multi-thread implementation

Because the execution speed of a single thread is slow, we add multiple threads to run the program,

typedef struct _tagValue{int start;int end;}PortNums;void _cdecl beginThreadFunc1(LPVOID lpParam) {PortNums *pnInt = (PortNums*)lpParam;scan("127.0.0.1", pnInt->start, pnInt->end);}int a(){PortNums m1;m1.start = 70;m1.end = 500;PortNums m2;m2.start = 501;m2.end = 1000;_beginthread(beginThreadFunc1, 0, &m1);_beginthread(beginThreadFunc1, 0, &m2);getchar();return 0;}

Note: # include <process. h>

 

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.