利用ICMP协议编写一个自己的Ping应用程序(用C++)

来源:百度知道 编辑:UC知道 时间:2024/09/21 13:24:48
在VC++环境中编写自己的Ping应用程序。

#pragma pack(4)

#define WIN32_LEAN_AND_MEAN
#include <WINSOCK2.H>
#include <STDIO.H>
#include <STDLIB.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 short frag_and_flags; // flags
unsigned char ttl;
unsigned char proto; // protocol (TCP, UDP etc)
unsigned short checksum; // IP checksum

unsigned int sourceIP;
unsigned int destIP;

}IpHeader;

//
// ICMP