编程实现ping命令_第1页
编程实现ping命令_第2页
编程实现ping命令_第3页
编程实现ping命令_第4页
编程实现ping命令_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

物联网工程学院实验报告课程名称《计算机网络》实验名称编程实现ping命令实验日期2023-5-1班级计科姓名学号03040实验报告要求1.实验名称2.实验要求3.实验环境4.实验步骤(写明实验结果)5.实验体会实验截图:实验代码:

#pragmapack(4)

#include

"winsock2.h"

#include

"stdlib.h"

#include

"stdio.h"

#defineICMP_ECHO8

#defineICMP_ECHOREPLY0

#defineICMP_MIN8//minimum8byteicmppacket(justheader)

/*TheIPheader*/

typedefstructiphdr{

unsignedinth_len:4;//lengthoftheheader

unsignedintversion:4;//VersionofIP

unsignedchartos;//Typeofservice

unsignedshorttotal_len;//totallengthofthepacket

typedefstructicmphdr{

BYTEi_type;

BYTEi_code;/*typesubcode*/

USHORTi_cksum;

USHORTi_id;

USHORTi_seq;

/*Thisisnotthestdheader,butwereservespacefortime*/

ULONGtimestamp;

}IcmpHeader;

#defineSTATUS_FAILED0xFFFF

#defineDEF_PACKET_SIZE

32

#defineDEF_PACKET_NUMBER

4

/*发送数据报的个数*/

#defineMAX_PACKET1024

#definexmalloc(s)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,(s))

#definexfree(p)HeapFree(GetProcessHeap(),0,(p))

voidfill_icmp_data(char*,int);

USHORTchecksum(USHORT*,int);

intdecode_resp(char*,int,structsockaddr_in*);

voidUsage(char*progname){

fprintf(stderr,"Usage:\n");

fprintf(stderr,"%s[numberofpackets][data_size]\n",progname);

fprintf(stderr,"datasizecanbeupto1Kb\n");

ExitProcess(STATUS_FAILED);

}

intmain(intargc,char**argv){

WSADATAwsaData;

SOCKETsockRaw;

structsockaddr_indest,from;

structhostent*hp;

intbread,datasize,times;

intfromlen=sizeof(from);

inttimeout=1000;

intstatistic=0;

/*用于统计结果*/

sockRaw=WSASocket(AF_INET,SOCK_RAW,IPPROTO_ICMP,NULL,0,WSA_FLAG_OVERLAPPED);

//

//注:为了使用发送接收超时设置(即设置SO_RCVTIMEO,SO_SNDTIMEO),

//

必须将标志位设为WSA_FLAG_OVERLAPPED!

//

if(sockRaw==INVALID_SOCKET){

fprintf(stderr,"WSASocket()failed:%d\n",WSAGetLastError());

ExitProcess(STATUS_FAILED);

}

bread=setsockopt(sockRaw,SOL_SOCKET,SO_RCVTIMEO,(char*)&timeout,

sizeof(timeout));

if(bread==SOCKET_ERROR){

fprintf(stderr,"failedtosetrecvtimeout:%d\n",WSAGetLastError());

ExitProcess(STATUS_FAILED);

}

timeout=1000;

bread=setsockopt(sockRaw,SOL_SOCKET,SO_SNDTIMEO,(char*)&timeout,

sizeof(timeout));

if(bread==SOCKET_ERROR){

fprintf(stderr,"failedtosetsendtimeout:%d\n",WSAGetLastError());

ExitProcess(STATUS_FAILED);

}

memset(&dest,0,sizeof(dest));

hp=gethostbyname(argv[1]);

if(!hp){

addr=inet_addr(argv[1]);

}

if((!hp)&&(addr==INADDR_NONE)){

fprintf(stderr,"Unabletoresolve%s\n",argv[1]);

ExitProcess(STATUS_FAILED);

}

if(hp!=NULL)

if(argc>2)

{

times=atoi(argv[2]);

if(times==0)

times=DEF_PACKET_NUMBER;

}

else

times=DEF_PACKET_NUMBER;

if(argc>3)

{

datasize=atoi(argv[3]);

if(datasize==0)

datasize=DEF_PACKET_SIZE;

if(datasize>1024)

/*用户给出的数据包大小太大*/

{

fprintf(stderr,"WARNING:data_sizeistoolarge!\n");

datasize=DEF_PACKET_SIZE;

}

}

else

datasize=DEF_PACKET_SIZE;

datasize+=sizeof(IcmpHeader);

icmp_data=(char*)xmalloc(MAX_PACKET);

recvbuf=(char*)xmalloc(MAX_PACKET);

if(!icmp_data){

fprintf(stderr,"HeapAllocfailed%d\n",GetLastError());

ExitProcess(STATUS_FAILED);

}

memset(icmp_data,0,MAX_PACKET);

fill_icmp_data(icmp_data,datasize);

//

//显示提示信息

//

fprintf(stdout,"\nPinging%s....\n\n",dest_ip);

continue;

}

fprintf(stderr,"sendtofailed:%d\n",WSAGetLastError());

ExitProcess(STATUS_FAILED);

}

if(bwrote<datasize){

fprintf(stdout,"Wrote%dbytes\n",bwrote);

}

bread=recvfrom(sockRaw,recvbuf,MAX_PACKET,0,(structsockaddr*)&from,&fromlen);

if(bread==SOCKET_ERROR){

if(WSAGetLastError()==WSAETIMEDOUT){

printf("Requesttimedout.\n");

continue;

}

fprintf(stderr,"recvfromfailed:%d\n",WSAGetLastError());

ExitProcess(STATUS_FAILED);

}

if(!decode_resp(recvbuf,bread,&from))

statistic++;/*成功接收的数目++*/

Sleep(1000);

}

/*

Displaythestatisticresult

*/

fprintf(stdout,"\nPingstatisticsfor%s\n",dest_ip);

fprintf(stdout,"

Packets:Sent=%d,Received=%d,Lost=%d(%2.0f%%loss)\n",times,

statistic,(times-statistic),(float)(times-statistic)/times*100);

unsignedshortiphdrlen;

iphdr=(IpHeader*)buf;

iphdrlen=(iphdr->h_len)*4;//numberof32-bitwords*4=bytes

if(bytes<iphdrlen+ICMP_MIN){

printf("Toofewbytesfrom%s\n",inet_ntoa(from->sin_addr));

}

icmphdr=(IcmpHeader*)(buf+iphdrlen);

if(icmphdr->i_type!=ICMP_ECHOREPLY){

fprintf(stderr,"non-echotype%drecvd\n",icmphdr->i_type);

return1;

}

if(icmphdr->i_id!=(USHORT)GetCurrentProcessId()){

fprintf(stderr,"someoneelse''spacket!\n");

return1;

}

printf("%dbytesfrom%s:",bytes,inet_ntoa(from->sin_addr));

printf("icmp_seq=%d.",icmphdr->i_seq);

printf("time:%dms",GetTickCount()-icmphdr->timestamp);

printf("\n");

return0;

}

USHORTchecksum(USHORT*buffer,intsize){

unsignedlongcksum=0;

while(size>1){

cksum+=*buffer++;

size-=sizeof(USHORT);

}

if(size){

cksum+=*(UCHAR*)buffer;

}

cksum=(cksum>>16)+(cksum&0xffff);

cksum+=(cksum>>16);

return(USHORT)(~cksum);

}

/*

HelperfunctiontofillinvariousstuffinourICMPrequest.

*/

voidfill_icmp_data(char*

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论