#include "stdafx.h"
#include <winsock2.h>
#include <iphlpapi.h>
#include <icmpapi.h>
#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"Iphlpapi.lib")
int _tmain(int argc, _TCHAR* argv[])
{
// Declare and initialize variables
int i;
HANDLE hIcmpFile;
unsigned long ipaddr = INADDR_NONE;
DWORD dwRetVal = 0;
char SendData[] = "Data Buffer";
LPVOID ReplyBuffer = NULL;
DWORD ReplySize = 0;
// Validate the parameters
/*if (argc != 2) {
printf("usage: %s IP address/n", argv[0]);
return 1;
}*/
ipaddr = inet_addr("192.168.0.1");
if (ipaddr == INADDR_NONE) {
printf("usage: %s IP address/n", argv[0]);
return 1;
}
hIcmpFile = IcmpCreateFile();
if (hIcmpFile == INVALID_HANDLE_VALUE) {
printf("/tUnable to open handle./n");
printf("IcmpCreatefile returned error: %ld/n", GetLastError() );
return 1;
}
ReplySize = sizeof(ICMP_ECHO_REPLY) + sizeof(SendData);
ReplyBuffer = (VOID*) malloc(ReplySize);
if (ReplyBuffer == NULL) {
printf("/tUnable to allocate memory/n");
return 1;
}
dwRetVal = IcmpSendEcho(hIcmpFile, ipaddr, SendData, sizeof(SendData),
NULL, ReplyBuffer, ReplySize, 1000);
if (dwRetVal != 0) {
PICMP_ECHO_REPLY pEchoReply = (PICMP_ECHO_REPLY)ReplyBuffer;
struct in_addr ReplyAddr;
ReplyAddr.S_un.S_addr = pEchoReply->Address;
printf("/tSent icmp message to %s/n", argv[1]);
if (dwRetVal > 1) {
printf("/tReceived %ld icmp message responses/n", dwRetVal);
printf("/tInformation from the first response:/n");
}
else {
printf("/tReceived %ld icmp message response/n", dwRetVal);
printf("/tInformation from this response:/n");
}
printf("/t Received from %s/n", inet_ntoa( ReplyAddr ) );
printf("/t Status = %ld/n",
pEchoReply->Status);
printf("/t Roundtrip time = %ld milliseconds/n",
pEchoReply->RoundTripTime);
printf("/t data = %s/n",pEchoReply->Data);
}
else {
printf("/tIcmpSendEcho returned error: %ld/n", GetLastError() );//出错之后,必须马上调用这个函数才能出现返回错误。
printf("/tCall to IcmpSendEcho failed./n"); return 1;
}
scanf("%d",&i);
return 0;
}