Windows multi-line threaded port scan

Source: Internet
Author: User

Not very familiar with multi-threading, the first simple application.

1 /*2 2015.5 HT3 multi-line threaded port scanning4 5 Netstat-an6 7 */8 9#include <iostream>Ten#include <process.h> One#include <winsock2.h> A #pragmaComment (lib, "Ws2_32.lib") - using namespacestd; -  the //port parameters passed by thread -typedefstruct  - { -UnsignedintMin_port; +UnsignedintMax_port; - }port; +Port *lpport =NewPort; A //Number of Threads at intThread_num; - //Mutex Amount - HANDLE Hmutex; -  - DWORD WINAPI Scan (lpvoid lpparameter) - { in     //Get Parameters -port* Port = (port*) Lpparameter; toSOCKET SOCKFD =NULL; + sockaddr_in sin; -sin.sin_family =af_inet; theSin.sin_addr. S_un. S_ADDR = inet_addr ("127.0.0.1"); *  $      for(unsignedinti = port->min_port; I <= port->max_port; ++i)Panax Notoginseng     { -         if(SOCKFD = =NULL) the         { +             //Creating Sockets ASOCKFD = socket (af_inet, Sock_stream,0); the             if(SOCKFD = =invalid_socket) +             { -cout <<"failed to create socket! "<<Endl; $                 return 0; $             } -         } -Sin.sin_port =htons (i); the         //connecting to a server -         intret = Connect (SOCKFD, (SOCKADDR *) &sin,sizeof(SOCKADDR));Wuyi         //request access to a mutex the WaitForSingleObject (Hmutex, INFINITE); -         if(ret = =socket_error) Wu         { -cout <<"Port:"<< I <<"Not Open!"<<Endl; About         } $         Else -         { -cout <<"Port:"<< I <<"Open!"<<Endl; - closesocket (SOCKFD); ASOCKFD =NULL; +         } the         //free access to a mutex - ReleaseMutex (Hmutex); $     } the  the     return 0; the } the  - intMain () in { the wsadata wsadata; the     if(WSAStartup (Makeword (2,2), &wsadata)! =0) About     { thecout <<"could not find the WinSock dll!"<<Endl; the         return 0; the     } +  -Hmutex =CreateMutex (NULL, FALSE, NULL); the Bayicout <<"Enter the start port and end port number:" ; theCIN >> Lpport->min_port >> lpport->Max_port; thecout <<"Number of input threads (less than £):"; -CIN >>Thread_num; -HANDLE handle[ $]; the     intScan_len = (lpport->max_port-lpport->min_port)/Thread_num; the      the      for(inti =0; i < Thread_num; ++i) the     { -Port *newport =NewPort; theNewport->min_port = Lpport->min_port + Scan_len *i; the         if(I < (Thread_num-1)) theNewport->max_port = Newport->min_port + Scan_len-1;94         Else the         { theNewport->max_port = lpport->Max_port; theHandle[i] = (handle) CreateThread (NULL,0, (lpthread_start_routine) scan, NewPort,0, NULL);98              Break; About         } -Handle[i] = (handle) CreateThread (NULL,0, (lpthread_start_routine) scan, NewPort,0, NULL);101     }102 waitformultipleobjects (Thread_num, handle, TRUE, INFINITE);103 104 CloseHandle (Hmutex); the     return 0;106}

Windows multi-line threaded port scan

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.