计算机网络ping的C语言代码

时间: 2023-10-28 22:05:25 浏览: 40
以下是计算机网络中ping的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <netdb.h> #include <netinet/in.h> #include <netinet/ip.h> #include <netinet/ip_icmp.h> #include <sys/time.h> #include <sys/socket.h> #include <sys/types.h> #include <unistd.h> #include <arpa/inet.h> #include <errno.h> #define PACKET_SIZE 4096 #define TIMEOUT 4 #define MAX_NO_PACKETS 3 unsigned short checksum(void *b, int len) { unsigned short *buf = 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 pack(int pack_no, char *sendpacket) { int i, packsize; struct icmp *icmp; struct timeval *tval; icmp = (struct icmp*)sendpacket; icmp->icmp_type = ICMP_ECHO; icmp->icmp_code = 0; icmp->icmp_cksum = 0; icmp->icmp_seq = pack_no; icmp->icmp_id = getpid(); packsize = 8 + 56; tval = (struct timeval*)icmp->icmp_data; gettimeofday(tval, NULL); icmp->icmp_cksum = checksum(sendpacket, packsize); return packsize; } int unpack(char *buf, int len, char *addr) { int i, iphdrlen, rtt; struct ip *ip; struct icmp *icmp; struct timeval *tvsend, tvrecv, tvresult; ip = (struct ip*)buf; iphdrlen = ip->ip_hl << 2; icmp = (struct icmp*)(buf + iphdrlen); len -= iphdrlen; if ( len < 8 ) { printf("ICMP packets\'s length is less than 8\n"); return -1; } if ( (icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == getpid())) { tvsend = (struct timeval*)icmp->icmp_data; gettimeofday(&tvrecv, NULL); tvresult = tvsub(tvrecv, *tvsend); rtt = tvresult.tv_sec * 1000 + tvresult.tv_usec / 1000; printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%d ms\n", len, addr, icmp->icmp_seq, ip->ip_ttl, rtt); return 0; } else return -1; } struct timeval tvsub(struct timeval timeval1, struct timeval timeval2) { struct timeval result; result.tv_sec = timeval1.tv_sec - timeval2.tv_sec; result.tv_usec = timeval1.tv_usec - timeval2.tv_usec; if (result.tv_usec < 0) { --result.tv_sec; result.tv_usec += 1000000; } return result; } int Ping(char *ipaddress) { struct hostent *host; struct icmp sendicmp; struct sockaddr_in from; struct sockaddr_in to; int from_len = 0; int sockfd = 0, nsend = 0, nreceived = 0, i = 0, packet_no = 0; char sendpacket[PACKET_SIZE] = {0}; char recvbuf[PACKET_SIZE] = {0}; memset(&from, 0, sizeof(from)); memset(&to, 0, sizeof(to)); if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0) { perror("socket error"); return -1; } setuid(getuid()); bzero(&sendicmp, sizeof(sendicmp)); to.sin_family = AF_INET; if ( inet_addr(ipaddress) == INADDR_NONE ) { if ((host = gethostbyname(ipaddress)) == NULL) { perror("gethostbyname error"); return -1; } memcpy(&to.sin_addr, host->h_addr, host->h_length); } else to.sin_addr.s_addr = inet_addr(ipaddress); printf("Ping %s ...\n", ipaddress); for ( i = 0; i < MAX_NO_PACKETS; i++) { nsend = pack(packet_no++, sendpacket); if ( sendto(sockfd, sendpacket, nsend, 0, (struct sockaddr*)&to, sizeof(to)) < 0 ) { perror("sendto error"); continue; } nreceived++; if ( recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr*)&from, &from_len) < 0 ) { perror("recvform error"); continue; } if ( unpack(recvbuf, sizeof(recvbuf), inet_ntoa(from.sin_addr)) == -1 ) continue; sleep(1); } printf("Ping statistics for %s:\n", ipaddress); printf("\tPackets: Sent = %d, Received = %d, Lost = %d(%d%% lost)\n", packet_no, nreceived, (packet_no - nreceived), ((packet_no - nreceived) * 100) / packet_no); close(sockfd); return 0; } int main(int argc, char *argv[]) { if ( argc < 2 ) { printf("Usage: %s IP\n", argv[0]); return -1; } Ping(argv[1]); return 0; } ``` 注意:此代码需要在 Linux 或 Mac 上编译运行,Windows 上需要进行一些修改才能编译运行。

相关推荐

最新推荐

recommend-type

Python测试网络连通性示例【基于ping】

主要介绍了Python测试网络连通性,结合实例形式分析了Python通过发送ping请求测试网络连通性相关操作技巧,需要的朋友可以参考下
recommend-type

C# ping网络IP 实现网络状态检测的方法

下面小编就为大家带来一篇C# ping网络IP 实现网络状态检测的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

网络课程设计(PING程序代码和FTP)

ping程序设计和文件传输协议的简单设计与实现(FTP) 用C语言编写 网络课程设计报告 网络课程设计报告 网络课程设计报告
recommend-type

Android中实现ping功能的多种方法详解

主要介绍了Android中实现ping功能的多种方法详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

【计算机网络】实验 IP协议与ICMP协议分析

计算机网络、实验、实验报告 wireshark基本操作、ARP协议分析、TCP、ICMP、wireshark、ping、网关、MAC地址、ARP协议、网络、计算机、机器、网络实验、实验
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

可见光定位LED及其供电硬件具体型号,广角镜头和探测器,实验设计具体流程步骤,

1. 可见光定位LED型号:一般可使用5mm或3mm的普通白色LED,也可以选择专门用于定位的LED,例如OSRAM公司的SFH 4715AS或Vishay公司的VLMU3500-385-120。 2. 供电硬件型号:可以使用常见的直流电源供电,也可以选择专门的LED驱动器,例如Meanwell公司的ELG-75-C或ELG-150-C系列。 3. 广角镜头和探测器型号:一般可采用广角透镜和CMOS摄像头或光电二极管探测器,例如Omron公司的B5W-LA或Murata公司的IRS-B210ST01。 4. 实验设计流程步骤: 1)确定实验目的和研究对象,例如车辆或机器人的定位和导航。
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。