//#include ping.h#include iostream#include stdio.h#include string#include string.h#include netinet/ip_icmp.h#include netdb.h#include sys/socket.h#include sys/types.h#include...

//#include "ping.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#define PACKET_SIZE 4096
#define SEND_DATA_LEN 56
#define ERROR -1
#define SUCCESS 1
#define MAX_WAIT_TIME 3
#define MAX_NO_PACKETS 4
#define NULL 0
using namespace std;
class Cping
{
public:
Cping(const char * ip, int timeout);
Cping(const Cping& org);
virtual ~Cping();
private:
std::string m_strIp;
std::string m_copy_Ip;
int m_nSend;
int m_nRecv;
struct sockaddr_in m_dest_addr;
struct sockaddr_in m_from_addr;
char m_sendpacket[PACKET_SIZE];
char m_recvpacket[PACKET_SIZE];
struct timeval m_tvrecv;
struct timeval m_begin_tvrecv;
struct timeval m_end_tvrecv;
double m_dTotalResponseTimes;
int m_nSocketfd;
int m_nMaxTimeWait;
public:
bool ping(int times);
bool CreateSocket();
bool CloseSocket();
void send_packet(void);
void recv_packet(void);
int pack(int pack_no);
int unpack(char *buf, int len);
void tv_sub(struct timeval *out, struct timeval *in);
void statistics(int sig);
static unsigned short cal_chksum(unsigned short *addr, int len);
};
Cping::Cping(const char *ip, int timeout)
{
m_strIp = ip;
m_copy_Ip = ip;
m_nSend = 0;
m_nRecv = 0;
m_dTotalResponseTimes = 0;
if(timeout > MAX_WAIT_TIME)
{
m_nMaxTimeWait = MAX_WAIT_TIME;
}
else
{
m_nMaxTimeWait = timeout;
}
}
Cping::~Cping()
{
if(!CloseSocket())
{
cout << "CloseSocket failed!" << endl;
}
}
bool Cping::ping(int times)
{
if(!CreateSocket())
{
printf("CreateSocket failed!!!\n");
return false;
}
printf("PING %s(%s): %d bytes data in ICMP packets.\n", m_strIp.c_str(),
m_copy_Ip.c_str(), SEND_DATA_LEN);
while(times--)
{
send_packet();
recv_packet();
sleep(1);
}
statistics(SIGINT);
return true;
}
bool Cping::CreateSocket()
{
char buf[2048];
int errnop = 0;
unsigned long inaddr;
struct hostent hostinfo, *dest_phost;
struct protoent *protocol = NULL;
if((protocol = getprotobyname("icmp")) == NULL)
{
perror("getprotobyname()");
printf("CreateSocket : getprotobyname failed:%d\n",errnop);
return false;
}
if(-1 == (m_nSocketfd = socket(AF_INET,SOCK_RAW,protocol->p_proto)))
{
perror("socket");
printf("CreateSocket: create socket failed:%d\n", m_nSocketfd);
return false;
}
setuid(getuid());
m_dest_addr.sin_family = AF_INET;
bzero(&(m_dest_addr.sin_zero),8);
if((inaddr = inet_addr(m_strIp.c_str())) == INADDR_NONE)
{
//if(getprotobyname_r(m_strIp.c_str(), &hostinfo, buf, sizeof(buf), &dest_phost, &errnop))
//{
// printf("CreateSocket: getprotobyname error %s failed:%d\n", m_strIp.c_str(),errnop);
// return false;
/
沃梦达教程
本文标题为:Linux C++实现ping指令


基础教程推荐
猜你喜欢
- C/C++编程中const的使用详解 2023-03-26
- C语言 structural body结构体详解用法 2022-12-06
- C语言基础全局变量与局部变量教程详解 2022-12-31
- C++中的atoi 函数简介 2023-01-05
- C++详细实现完整图书管理功能 2023-04-04
- 详解c# Emit技术 2023-03-25
- C利用语言实现数据结构之队列 2022-11-22
- C++使用easyX库实现三星环绕效果流程详解 2023-06-26
- 一文带你了解C++中的字符替换方法 2023-07-20
- 如何C++使用模板特化功能 2023-03-05