Ceci est une version très basique de la fonction Ping.
Elle ne gère pas tous les cas, mais je vais poster une version améliorée bientôt.
#include <winsock2.h>
#include <Ipexport.h>
#include <icmpapi.h>
#pragma comment(lib, "iphlpapi.lib")
#pragma comment(lib, "ws2_32.lib")
int IsPingable(const char *host, int timeout)
{
IP_OPTION_INFORMATION ioi;
ICMP_ECHO_REPLY ier;
LPHOSTENT Host;
WSADATA wsaData;
IN_ADDR iaAddr;
HANDLE hIcmp;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
WSACleanup();
return (0);
}
iaAddr.s_addr = inet_addr(host);
if (iaAddr.s_addr == INADDR_NONE)
Host = gethostbyname(host);
else
Host = gethostbyaddr((char *)&iaAddr, sizeof(IN_ADDR), AF_INET);
if (Host == NULL)
{
WSACleanup();
return (0);
}
hIcmp = IcmpCreateFile();
ioi.Ttl = 255;
ioi.Tos = 0;
ioi.Flags = 0;
ioi.OptionsSize = 0;
ioi.OptionsData = NULL;
IcmpSendEcho(hIcmp, *(DWORD *)(*Host->h_addr_list), NULL, 0, &ioi, &ier, sizeof(ICMP_ECHO_REPLY), timeout);
IcmpCloseHandle(hIcmp);
WSACleanup();
return (ier.Status == 0);
}
Exemple:int main(int argc, char **argv)
{
printf("IsPingable : %d\n", IsPingable("clustrmaps.com", 5000));
getchar();
return (EXIT_SUCCESS);
}


