用c写一个一直 ping 一个ip地址 如果ping不到就给ret值为1
时间: 2023-06-19 20:06:04 浏览: 167
C语言中判断两个IPv4地址是否属于同一个子网的代码
以下是一个简单的 C 语言程序,可以一直 ping 一个 IP 地址,如果 ping 不通则返回 1:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
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 ping(char *host)
{
struct hostent *hname;
struct sockaddr_in addr_ping;
int sockfd, i, len, flag = 1, flag_recv = 1;
struct packet pkt;
struct timeval tv_begin, tv_end, tv_interval;
float rtt = 0;
memset(&addr_ping, 0, sizeof(addr_ping));
addr_ping.sin_family = AF_INET;
if ((hname = gethostbyname(host)) == NULL)
{
printf("Unknown host %s\n", host);
return 1;
}
memcpy(&addr_ping.sin_addr, hname->h_addr, sizeof(addr_ping.sin_addr));
printf("PING %s (%s): %d bytes data in ICMP packets.\n", hname->h_name, inet_ntoa(addr_ping.sin_addr), (int)sizeof(struct packet));
if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0)
{
printf("socket error\n");
return 1;
}
while (flag)
{
flag_recv = 1;
memset(&pkt, 0, sizeof(pkt));
pkt.hdr.type = ICMP_ECHO;
pkt.hdr.code = 0;
pkt.hdr.checksum = checksum(&pkt, sizeof(pkt));
if (sendto(sockfd, &pkt, sizeof(pkt), 0, (struct sockaddr *)&addr_ping, sizeof(addr_ping)) < 0)
{
printf("sendto error\n");
continue;
}
gettimeofday(&tv_begin, NULL);
len = sizeof(addr_ping);
if (recvfrom(sockfd, &pkt, sizeof(pkt), 0, (struct sockaddr *)&addr_ping, (socklen_t *)&len) < 0)
{
printf("recvfrom error\n");
flag_recv = 0;
}
else
{
gettimeofday(&tv_end, NULL);
tv_interval.tv_sec = tv_end.tv_sec - tv_begin.tv_sec;
tv_interval.tv_usec = tv_end.tv_usec - tv_begin.tv_usec;
rtt = tv_interval.tv_sec * 1000.0 + tv_interval.tv_usec / 1000.0;
printf("%d bytes from %s: icmp_seq=%u ttl=%d time=%.1f ms\n", sizeof(pkt), inet_ntoa(addr_ping.sin_addr), pkt.hdr.un.echo.sequence, pkt.hdr.un.echo.sequence, rtt);
flag_recv = 1;
}
sleep(1);
if (!flag_recv)
{
flag = 0;
printf("ping %s fail\n", host);
close(sockfd);
return 1;
}
if (i >= MAX_NO_PACKETS)
{
flag = 0;
}
}
close(sockfd);
return 0;
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: %s <hostname>\n", argv[0]);
return 1;
}
return ping(argv[1]);
}
```
注意:该程序需要在 Linux 环境下运行,并且需要 root 权限才能发送 ICMP 包。
阅读全文