linux c++ ping
时间: 2023-07-18 10:47:43 浏览: 163
linux系统下用C程序实现Ping功能.rar_linux_linux c++ 系统 源码_linux ping_ping l
你想要了解如何在 Linux C++ 中实现 ping 功能吗?如果是的话,下面是一个简单的示例代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/ip_icmp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>
#define PACKET_SIZE 4096
struct packet
{
struct icmphdr hdr;
char msg[PACKET_SIZE-sizeof(struct icmphdr)];
};
unsigned short checksum(void *b, int len)
{
unsigned short *buf = (unsigned short *)b;
unsigned int sum = 0;
unsigned short result;
for (sum = 0; len > 1; len -= 2)
{
sum += *buf++;
}
if (len == 1)
{
sum += *(unsigned char *)buf;
}
sum = (sum >> 16) + (sum & 0xFFFF);
sum += (sum >> 16);
result = ~sum;
return result;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <hostname/IP address>\n", argv[0]);
return 1;
}
struct hostent *host = gethostbyname(argv[1]);
if (host == NULL)
{
printf("Failed to resolve hostname\n");
return 1;
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = 0;
addr.sin_addr.s_addr = *(unsigned long *)host->h_addr_list[0];
int sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
if (sockfd < 0)
{
printf("Failed to create socket\n");
return 1;
}
struct timeval tv;
tv.tv_sec = 5;
tv.tv_usec = 0;
if (setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv)) < 0)
{
printf("Failed to set socket options\n");
return 1;
}
struct packet pckt;
memset(&pckt, 0, sizeof(pckt));
pckt.hdr.type = ICMP_ECHO;
pckt.hdr.un.echo.id = getpid();
memset(pckt.msg, 'a', sizeof(pckt.msg)-1);
pckt.msg[sizeof(pckt.msg)-1] = 0;
pckt.hdr.un.echo.sequence = 0;
pckt.hdr.checksum = checksum(&pckt, sizeof(pckt));
struct sockaddr_in r_addr;
socklen_t r_addr_len = sizeof(r_addr);
struct timeval start;
gettimeofday(&start, NULL);
int sent = sendto(sockfd, &pckt, sizeof(pckt), 0, (struct sockaddr*)&addr, sizeof(addr));
if (sent <= 0)
{
printf("Failed to send ICMP packet\n");
return 1;
}
char buf[PACKET_SIZE];
int received = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&r_addr, &r_addr_len);
if (received <= 0)
{
printf("Failed to receive ICMP packet\n");
return 1;
}
struct timeval end;
gettimeofday(&end, NULL);
double rtt = (double)(end.tv_sec-start.tv_sec)*1000.0 + (double)(end.tv_usec-start.tv_usec)/1000.0;
printf("Received ICMP packet from %s, rtt: %f ms\n", inet_ntoa(r_addr.sin_addr), rtt);
close(sockfd);
return 0;
}
```
这个代码可以通过 ICMP 协议向目标主机发送 ping 命令,并接收回复,从而计算出 RTT(Round Trip Time)即往返时间。需要注意的是,这个代码需要在 root 权限下运行,否则会出现权限不足的错误。
阅读全文