ping&telnet.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // 必要的头文件和lib;
  2. #include <Winsock2.h>
  3. #include <iphlpapi.h>
  4. #include <icmpapi.h>
  5. #pragma comment(lib,"Iphlpapi.lib")
  6. #pragma comment(lib,"Ws2_32.lib")
  7. bool ping(LPCSTR lpIPAddress)
  8. {
  9. bool bresult = false;
  10. if (lpIPAddress == NULL )
  11. return bresult;
  12. HANDLE hIcmpFile;
  13. unsigned long ipaddr = INADDR_NONE;
  14. DWORD dwRetVal = 0;
  15. char SendData[] = "Data Buffer";
  16. LPVOID ReplyBuffer = NULL;
  17. DWORD ReplySize = 0;
  18. if(lpIPAddress[0] >= '0' && lpIPAddress[0] <= '9')
  19. {//4段十进制;
  20. ipaddr = inet_addr(lpIPAddress);
  21. if(ipaddr == INADDR_NONE)
  22. {// 地址无效;
  23. return false;
  24. }
  25. }
  26. else
  27. {//计算机名;
  28. // 初始化socket;
  29. WSADATA WSAData = { 0 };
  30. if(0 != WSAStartup(MAKEWORD(2, 2), &WSAData))
  31. return false;
  32. // gethostbyname前必须WSAStartup;
  33. hostent *phostent = gethostbyname(lpIPAddress);
  34. if(phostent == NULL)
  35. return false;
  36. ipaddr = *(DWORD*)phostent->h_addr_list[0];
  37. }
  38. // 创建imcp协议句柄;
  39. hIcmpFile = IcmpCreateFile();
  40. if (hIcmpFile == INVALID_HANDLE_VALUE)
  41. return bresult;
  42. ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
  43. ReplyBuffer = (VOID*) malloc(ReplySize);
  44. if (ReplyBuffer == NULL)
  45. return bresult;
  46. // 发送数据;
  47. dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData), NULL, ReplyBuffer, ReplySize, 1000);
  48. if (dwRetVal != 0)
  49. {
  50. PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
  51. if ( pEchoReply->Status == 0 )
  52. {
  53. bresult = true;
  54. }
  55. }
  56. if (ReplyBuffer)
  57. free(ReplyBuffer);
  58. return bresult;
  59. }
  60. bool telnet(IN LPCCH lpIPAddress, IN DWORD dwPort)
  61. {
  62. // 初始化socket;
  63. WSADATA WSAData = { 0 };
  64. if(0 != WSAStartup(MAKEWORD(2, 2), &WSAData))
  65. return false;
  66. // 创建客户端;
  67. SOCKET sock;
  68. SOCKADDR_IN srvaddr;
  69. sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  70. if(sock == INVALID_SOCKET)
  71. {// 创建套接字失败;
  72. WSACleanup();
  73. return false;
  74. }
  75. // 填充address;
  76. if(lpIPAddress[0] >= '0' && lpIPAddress[0] <= '9')
  77. {//4段十进制;
  78. srvaddr.sin_addr.S_un.S_addr = inet_addr(lpIPAddress);
  79. if(srvaddr.sin_addr.S_un.S_addr == INADDR_NONE)
  80. {// 地址无效;
  81. closesocket(sock);
  82. WSACleanup();
  83. return false;
  84. }
  85. }
  86. else
  87. {//计算机名;
  88. hostent *phostent = gethostbyname(lpIPAddress);
  89. if(phostent == NULL)
  90. {
  91. closesocket(sock);
  92. WSACleanup();
  93. return false;
  94. }
  95. srvaddr.sin_addr.S_un.S_addr = *(DWORD*)phostent->h_addr_list[0];
  96. }
  97. srvaddr.sin_port = htons(dwPort);
  98. srvaddr.sin_family = AF_INET;
  99. // 连接服务器;
  100. if( SOCKET_ERROR == connect(sock, (sockaddr*)&srvaddr, sizeof(SOCKADDR_IN)) )
  101. {// 连接失败,认为telnet失败;
  102. closesocket(sock);
  103. WSACleanup();
  104. return false;
  105. }
  106. // 释放资源;
  107. closesocket(sock);
  108. WSACleanup();
  109. return true;
  110. }