-
大小: 8KB文件类型: .cpp金币: 1下载: 0 次发布日期: 2021-06-10
- 语言: C/C++
- 标签: winsockping
资源简介
我的作业 需要写作业的可以参考
代码片段和文件信息
/*
* Renkai_ping.cpp
*
* Created on: 2012-5-19
* Author: renkai
*/
#include
#include
#include
#include “winsock2.h“
#include “stdlib.h“
#include “stdio.h“
#include
using namespace std;
#define ICMP_ECHO 8
#define ICMP_ECHOREPLY 0
#define ICMP_MIN 8 //Minimum 8-byte ICMP packet
//IP首部数据结构
typedef struct iphdr
{
unsigned int h_len :4; //首部长度
unsigned int version :4; //版本
unsigned char tos; //服务类型
unsigned short total_len; //报文总长度
unsigned short ident; //标识
unsigned short frag_and_flags; //偏移量
unsigned char ttl; //寿命
unsigned char proto; //协议
unsigned short checksum; // 首部校验和
unsigned int sourceIP; // 源站IP
unsigned int destIP; // 目的站IP
} IpHeader;
//ICMP首部数据结构
typedef struct icmphdr
{
BYTE i_type; //类型
BYTE i_code; //代码
USHORT i_cksum; //首部校验和
USHORT i_id; //标识
USHORT i_seq; //序列号
ULONG timestamp; //时间戳
} IcmpHeader;
#define STATUS_FAILED 0xFFFF
#define DEF_PACKET_SIZE 32 //默认发送包的长度
#define DEF_PACKET_NUMBER 6 //默认发送包的数量
#define MAX_PACKET 1024 //最大ICMP发送包的长度
void
fill_icmp_data(char* int int); //填充ICMP报头
USHORT
checksum(USHORT* int); //计算ICMP首部校验和
int
decode_resp(char* int struct sockaddr_in*); //解析收到的数据包
int main(int argcchar* argv[])
{
if (argc == 1)
{
cout << “欢迎使用Renkai的ping“ << endl;
cout << “使用方法如下“ << endl;
cout << “ping [-n count] [-l size] [-s timestamp] [-w timeout] ip地址或者域名“
<< endl << “[]中的参数为可选项“ << endl;
return -1;
}
USHORT seq_no=0;
int ts=0statistic=0;
int breaddatasize=32packnum=6;
setbuf(stdoutNULL);
WORD wVersionRequested;
WSADATA wsaData;
int timeout=1000;
//处理参数
for (int j = 1; j < argc-1;j++)
{
int temp;
temp = strcmp(argv[j] “-n“);
if (temp == 0)
{
j++;
packnum = atoi(argv[j]);
cout << “You have set packnum “ << packnum << endl;
continue;
}
temp = strcmp(argv[j] “-l“);
if (temp == 0)
{
j++;
datasize = atoi(argv[j]);
cout << “You have set datasize “ << datasize << endl;
continue;
}
temp = strcmp(argv[j] “-w“);
if (temp == 0)
{
j++;
timeout = atoi(argv[j]);
cout << “You have set timeout “ << timeout << endl;
continue;
}
temp = strcmp(argv[j] “-s“);
if (temp == 0)
{
j++;
ts = atoi(argv[j]);
cout << “You have set timestamp “ << ts << endl;
continue;
}
}
if (packnum <= 0)
{
cout << “too small packnumit must larger than 0“;
return -1;
}
if (datasize < 32 || datasize > 1024)
{
cout << “too small or big datasizeit must be in 32-1024“;
return -1;
}
if (timeout < 1000 || timeout > 10000)
- 上一篇:基于Linux的VIM编辑器超炫配置
- 下一篇:计算机图形学 直线裁剪算法
评论
共有 条评论