tcp_saomiao.cpp
上传用户:whjwfkj
上传日期:2021-07-07
资源大小:8k
文件大小:2k
源码类别:

扫描程序

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <winsock2.h>
  4. #pragma comment(lib,"ws2_32.lib")
  5. int main(int argc,char **argv)
  6. {
  7. char *TargetIPAddr="172.19.26.15";
  8. unsigned int StartPort=80;
  9. unsigned int EndPort=85;
  10. SOCKET ScanSocket;
  11. struct sockaddr_in TargetAddr_in;
  12. int Ret;
  13. HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
  14. CONSOLE_SCREEN_BUFFER_INFO bInfo;
  15. GetConsoleScreenBufferInfo(hCon,&bInfo);
  16. WSADATA wsaData;
  17. if((Ret=WSAStartup(MAKEWORD(2,1),&wsaData))!=0)
  18. {
  19. printf("WSAStartup failed with error %dn",Ret);
  20. return 0;
  21. }
  22. DWORD dwStart=GetTickCount();
  23. printf("Target IP:%sn",TargetIPAddr);
  24. for(unsigned int i=StartPort;i<=EndPort;i++)
  25. {
  26. ScanSocket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  27. if(ScanSocket==INVALID_SOCKET)
  28. {
  29. printf("socket failed with error:%dn",WSAGetLastError());
  30. return 0;
  31. }
  32. TargetAddr_in.sin_family=AF_INET;
  33. TargetAddr_in.sin_addr.s_addr=inet_addr(TargetIPAddr);
  34. TargetAddr_in.sin_port=htons(i);
  35. if(connect(ScanSocket,(struct sockaddr*)&TargetAddr_in,sizeof(TargetAddr_in))==SOCKET_ERROR)
  36. {
  37. SetConsoleTextAttribute(hCon,10);
  38. printf("Port%5d Closen",i);
  39. }
  40. else
  41. {
  42. SetConsoleTextAttribute(hCon,14);
  43. printf("Port%5d Openn",i);
  44. }
  45. if(closesocket(ScanSocket)==SOCKET_ERROR)
  46. {
  47. printf("closesocket failed with error %dn",WSAGetLastError());
  48. return 0;
  49. }
  50. }
  51. SetConsoleTextAttribute(hCon,14);
  52. printf("ntime:%dmsn",GetTickCount()-dwStart);
  53. SetConsoleTextAttribute(hCon,bInfo.wAttributes);
  54. if(WSACleanup()==SOCKET_ERROR)
  55. {
  56. printf("WSACleanip failed with error %dn",WSAGetLastError());
  57. return 0;
  58. }
  59. return 1;
  60. }