多線程編程執行個體2

來源:互聯網
上載者:User
//多線程連接埠掃描#include <WinSock2.h>#include <stdio.h>#include <windows.h>#include <process.h>#pragma  comment(lib,"WS2_32")char ip[20];DWORD WINAPI scan(LPVOID lpParam){int prot = *(int*)lpParam;sockaddr_in sin;sin.sin_family = AF_INET;sin.sin_addr.S_un.S_addr = inet_addr(ip);SOCKET s = ::socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);sin.sin_port = htons(prot);if (::connect(s,(sockaddr *)&sin,sizeof(sin)) == SOCKET_ERROR ){::closesocket(s);return 0;}else {printf("%4d 連接埠開啟!\n",prot);::closesocket(s);return 0;}}int main(){WSADATA wsaData;WORD sockVersion = MAKEWORD(2,0);::WSAStartup(sockVersion,&wsaData);int i;HANDLE h[1000];DWORD dwThreadId;printf("請輸入目標IP地址:");scanf("%s",ip);printf("...........掃描1~2000號連接埠(TCP)...........\n");for (i = 1;i<=1000;i++){h[i-1] = CreateThread(NULL,0,scan,(LPVOID)new int(i),0,&dwThreadId);}::WaitForMultipleObjects(1000,h,FALSE,INFINITE);for (i = 0;i<1000;i++){::CloseHandle(h[i]);}for (i = 1000;i<=2000;i++){h[i-1-1000] = ::CreateThread(NULL,0,scan,(LPVOID)new int(i),0,&dwThreadId);}::WaitForMultipleObjects(1000,h,FALSE,INFINITE);for (i = 0;i<1000;i++)   {   ::CloseHandle(h[i]);}::WSACleanup();printf(".....................掃描結束!............................\n");system("pause");return 0;}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.