編程實現(xiàn)ping命令_第1頁
編程實現(xiàn)ping命令_第2頁
編程實現(xiàn)ping命令_第3頁
編程實現(xiàn)ping命令_第4頁
編程實現(xiàn)ping命令_第5頁
已閱讀5頁,還剩12頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

編程實現(xiàn)ping命令物聯(lián)網(wǎng)工程學(xué)院實驗報告課程名稱《計算機(jī)網(wǎng)絡(luò)》實驗名稱編程實現(xiàn)ping命令實驗日期2023-5-1班級計科姓名學(xué)號03040實驗報告要求1.實驗名稱2.實驗要求3.實驗環(huán)境4.實驗步驟(寫明實驗結(jié)果)5.實驗體會實驗3編程實現(xiàn)ping命令實驗截圖:實驗代碼:

#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

unsignedshortident;//uniqueidentifier

unsignedshortfrag_and_flags;//flags

unsignedcharttl;

unsignedcharproto;//protocol(TCP,UDPetc)

unsignedshortchecksum;//IPchecksum

unsignedintsourceIP;

unsignedintdestIP;

}IpHeader;

//

//ICMPheader

//

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

/*發(fā)送數(shù)據(jù)報的個數(shù)*/

#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;

/*用于統(tǒng)計結(jié)果*/

char*dest_ip;

char*icmp_data;

char*recvbuf;

unsignedintaddr=0;

USHORTseq_no=0;

if(WSAStartup(MAKEWORD(2,1),&wsaData)!=0){

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

ExitProcess(STATUS_FAILED);

}

if(argc<2){

Usage(argv[0]);

}

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

//

//注:為了使用發(fā)送接收超時設(shè)置(即設(shè)置SO_RCVTIMEO,SO_SNDTIMEO),

//

必須將標(biāo)志位設(shè)為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)

memcpy(&(dest.sin_addr),hp->h_addr,hp->h_length);

else

dest.sin_addr.s_addr=addr;

if(hp)

dest.sin_family=hp->h_addrtype;

else

dest.sin_family=AF_INET;

dest_ip=inet_ntoa(dest.sin_addr);

//

//

atoi函數(shù)原型是:intatoi(constchar*string);

//

Thereturnvalueis0iftheinputcannotbeconvertedtoaninteger!

//

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)

/*用戶給出的數(shù)據(jù)包大小太大*/

{

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);

for(inti=0;i{

intbwrote;

((IcmpHeader*)icmp_data)->i_cksum=0;

((IcmpHeader*)icmp_data)->timestamp=GetTickCount();

((IcmpHeader*)icmp_data)->i_seq=seq_no++;

((IcmpHeader*)icmp_data)->i_cksum=checksum((USHORT*)icmp_data,datasize);

bwrote=sendto(sockRaw,icmp_data,datasize,0,(structsockaddr*)&dest,sizeof(dest));

if(bwrote==SOCKET_ERROR){

if(WSAGetLastError()==WSAETIMEDOUT){

printf("Requesttimedout.\n");

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++;/*成功接收的數(shù)目++*/

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);

WSACleanup();

return0;

}

/*

TheresponseisanIPpacket.WemustdecodetheIPheadertolocate

theICMPdata

*/

intdecode_resp(char*buf,intbytes,structsockaddr_in*from){

IpHeader*iphdr;

IcmpHeader*icmphdr;

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=

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論