-
大小: 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“?<??????cout?<“使用方法如下“?<??????cout?<“ping?[-n?count]?[-l?size]?[-s?timestamp]?[-w?timeout]?ip地址或者域名“
??????????<??????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?????{
??????int?temp;
??????temp?=?strcmp(argv[j]?“-n“);
??????if?(temp?==?0)
????????{
??????????j++;
??????????packnum?=?atoi(argv[j]);
??????????cout?<“You?have?set?packnum?“?<??????????continue;
????????}
??????temp?=?strcmp(argv[j]?“-l“);
??????if?(temp?==?0)
????????{
??????????j++;
??????????datasize?=?atoi(argv[j]);
??????????cout?<“You?have?set?datasize?“?<??????????continue;
????????}
??????temp?=?strcmp(argv[j]?“-w“);
??????if?(temp?==?0)
????????{
??????????j++;
??????????timeout?=?atoi(argv[j]);
??????????cout?<“You?have?set?timeout?“?<??????????continue;
????????}
??????temp?=?strcmp(argv[j]?“-s“);
??????if?(temp?==?0)
????????{
??????????j++;
??????????ts?=?atoi(argv[j]);
??????????cout?<“You?have?set?timestamp?“?<??????????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編輯器超炫配置
- 下一篇:計算機圖形學 直線裁剪算法
評論
共有 條評論