//#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++实现ETW进行进程变动监控详解 2023-05-15
- C语言编程C++旋转字符操作串示例详解 2022-11-20
- [C语言]二叉搜索树 2023-09-07
- centos 7 vscode cmake 编译c++工程 2023-09-17
- 带你深度走入C语言取整以及4种函数 2022-09-17
- C语言实现宾馆管理系统课程设计 2023-03-13
- [c语言-函数]不定量参数 2023-09-08
- C++实战之二进制数据处理与封装 2023-05-29
- 全面了解C语言 static 关键字 2023-03-26
- C语言 详解字符串基础 2023-03-27