端口扫描(TCP)
还待优化。。。
1 #include <string.h> 2 #include <WinSock.h> 3 #include <stdio.h> 4 #pragma comment(lib,"ws2_32.lib") 5 int main(int argc, _TCHAR* argv[]) { 6 int i=0; 7 int portArr[]={21,23,25,53,67,68,69,79,80,99,109,110,111,113,119,135,137,139,143,161,443,554,1024,3389,4489,8080,8088}; 8 int portArrLen=sizeof(portArr)/sizeof(portArr[0]); 9 //初始化Windows Sockets 动态 10 WSADATA wsaData; 11 if(WSAStartup(MAKEWORD(2,2),&wsaData)!=0) { 12 printf("找不到可使用的WinSock dll!\n"); 13 return 1; 14 } 15 16 SOCKET sClient = NULL;//Linux 中ocket为 int类型 17 18 struct sockaddr_in addrServ;//套接字地址结构 19 addrServ.sin_family=AF_INET; 20 addrServ.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");//对本机进行扫描 21 22 for (i = 0; i<portArrLen; i++) { 23 24 addrServ.sin_port=htons(portArr[i]);//设置端口 25 sClient = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);//赋值套接字 26 27 //连接服务器, 若connect失败则该套接字不可再用,必须关闭 28 if( (connect(sClient,(struct sockaddr_in *)&addrServ,sizeof(addrServ))) ==0) { 29 printf("%d pen!\n",portArr[i]); 30 }else{ 31 printf("%d not pen!\n",portArr[i]); 32 } 33 //无论是否连接成功,我们都关闭套接字 34 closesocket(sClient); 35 sClient=NULL; 36 } 37 puts("扫描结束!"); 38 getchar(); 39 //终止Winsock 2 DLL (Ws2_32.dll) 的使用. 40 WSACleanup(); 41 return 0; 42 }