c语言ping程序中文注释

来源:百度知道 编辑:UC知道 时间:2024/09/20 13:48:58
关于raw socket的

编写自己的一个ping程序,可以说是许多人迈出网络编程的第一步吧!!这个ping程序的源代码经过我的修改和调试,基本上可以取代windows中自带的ping程序. 各个模块后都有我的详细注释和修改日志,希望能够对大家的学习有所帮助!!

/* 本程序的主要源代码来自MSDN网站, 笔者只是做了一些改进和注释! 另外需要注意的是在Build之前,必须加入ws2_32.lib库文件,否则会提示"error LNK2001:"的错误!*/
Version 1.1 修改记录:
<1> 解决了socket阻塞的问题,从而能够正确地处理超时的请求!
<2> 增加了由用户控制发送ICMP包的数目的功能(即命令的第二个参数)
<3> 增加了对ping结果的统计功能.

#pragma pack(4)
#include "winsock2.h"
#include "stdlib.h"
#include "stdio.h"
#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0
#define ICMP_MIN 8 // minimum 8 byte icmp packet (just header)
/* The IP header */
typedef struct iphdr {
unsigned int h_len:4; // length of the header
unsigned int version:4; // Version of IP
unsigned char tos; // Type of service
unsigned short total_len; // total length of the packet
unsigned short ident; // unique identifier
unsigned s