/*
AliveChecker.h
author: zhouciming@163.com
*/
#ifndef __ALIVE_CHECKER_H__
#define __ALIVE_CHECKER_H__
typedef unsigned short ushort;
typedef unsigned long ulong;
class CPing
{
public:
CPing();
~CPing();
bool ping(ulong destIP, int timeout = 10000);
protected:
bool init();
ushort getCheckSum(ushort* addr, int len); // 计算校验和
private:
int m_sockfd;
};
#endif
/*
AliveChecker.cpp
author: zhouciming@163.com
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <netinet/ip_icmp.h>
#include "AliveChecker.h"
#define INVALID_SOCKET -1
#define PACKET_SIZE 1024
CPing::CPing()
{
m_sockfd = INVALID_SOCKET;
}
CPing::~CPing()
{
if(m_sockfd != INVALID_SOCKET)
close(m_sockfd);
}
bool CPing::init() // 初始化socket fd
{
if(m_sockfd !=