Ping應(yīng)用程序?qū)崿F(xiàn)代碼_第1頁
Ping應(yīng)用程序?qū)崿F(xiàn)代碼_第2頁
Ping應(yīng)用程序?qū)崿F(xiàn)代碼_第3頁
Ping應(yīng)用程序?qū)崿F(xiàn)代碼_第4頁
Ping應(yīng)用程序?qū)崿F(xiàn)代碼_第5頁
已閱讀5頁,還剩4頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡介

1、ping應(yīng)用程序?qū)崿F(xiàn)代碼#include<stdio.h>#include<stdlib.h>#includcwindows. h>#includcproccss.h>#includc "winsock. htfpragma comment (lib, "ws2_32");ttdefine send_size 32ttdefine packet size 4096ttdefine icmp_ech0 8 #define icmp_echoreply 0struct icmp!unsigned char icmp_type;uns

2、igned char icmpcode;unsigned short icmp_cksum;unsigned short icmp_id;unsigned short icmp_seq;unsigned long icmpdata;struct ipunsigned char ip_h1:4;unsigned char ip_v:4;unsigned char ip tos;unsigned short ip_len;unsigned short ip_id;unsigned short ip_off;unsigned char ip_ttl;unsigned char ip_p;unsign

3、ed short ip sum;unsigned long ip src;unsigned long ip_dst;unsigned char sendpacketpacket_size; unsigned char recvpacketpacket_size; struct sockaddr_in dest_addr; struct sockaddr_in fromaddr; int sockfd;int pid;unsigned short cal_chksum(unsigned short *addr, int len);int pack(int pack_no);int unpack(

4、unsigned char *buf, int len); void send_packet(void); void rccv_packet(void);void main()iint argc;char *argv2;argc = 2;argv0= -t;argvl="192. 168. 1.2"struct hostent *host;struct protoent *protocol; wsadata wsadata;int timeout二1000;int send_c0unt=4;int i;char *par host;par host=argvargc-l;

5、/ip?0switch(argc)icase 2: break;case 3: if (slrcmpqrgvll, "-1)二二0)isend_count二10000;break;/fal1 throughdefauit:printf ("usage: %s -1 host name or ip addressnz/, argv0);exit (1);if(wsastartup(0x1010, &wsadata)!=0) printf (z/wsastartup errorn,z);exit (1);if( (protocol=getprotobyname (/zi

6、cmpz/) ) =null) printf (zzgetprotobyname errornz/);exit (1);printf (z,%sn,z, protocol->p name);printf (z/%snz,, protocol->p aliases);printf (z,%dn,z, protocol->p_proto);system(pause);if( (sockfd二socket(af inet, sock raw, protocol->p_proto) )<0)i:printf ("socket errorn);exit (1);i

7、f(setsockopt(sockfd, sol socket, so rcvtimeo, (char*)&timeout, sizeof(timeout)<0)fprintf (stderr, /zfai 1 ed to set recvtimeout: %dn, wsagetlasterror();if(setsockopt(sockfd, s0l_s0cket, so_sndtimeo, (char*)&timeout, sizeof(timeout) )<0)fprintf (stderr, ''failed to set sendtimeo

8、ut: %dn,z, wsagetlasterror ();memset (&dest addr, 0, sizeof(dest addr);dest addr. sin family=af inet;printf (sn,par host);if (host二gelhostbyname (par_host)iprintf ("%sn: par_host);memcpy ( (char *)&dest_addr. sin_addr, host-h_addr, host->h_length);/resolve address to hostnameif(host=

9、gethostbyaddr (host->h addr,4,pf inet)par host二host->h name;printf(%sn,par_host); elseif( dest_addr. sin_addr. s_addr=inet_addr(par_host)=inaddr_none)iprintf (z,unkown host %spar host);exit (1);pid=getpid();printf ("%d,pid);system (/zpausez,);printf c'pinging %s %s: with %d bytes ofda

10、ta:nnz par_host, inet_ntoa(dest_addr. sin_addr), send_size);for(i=0;i<send_count;i+)isend_packet();recv_packet();sleep (1000);/this algorithm is referenced from other's/*d?neea?*/unsigned short cal chksum(unsigned short *addr, int len)iint nleft二len;int sum=0;unsigned short *w=addr;unsigned s

11、hort answer二0;while(nleft>l) sum+二*w+;nleft-=2;if( nleft二二1) *(unsigned char *)(&answer)二*(unsigned char *)w; sum+=answcr;sum= (sum»16) + (sum&oxffff);sum+=(sum>>16);2answer= sum;return answer;/?d?uint pack (int pack_no)iint packsize;struct icmp *icmp;packsize=8+send size;icmp=

12、 (struct icmp*)sendpacket;icmp->icmp type二icmp echo;i cmp-> i cmp_code=0;i cmp-> i cmpck s uni=0;icmp->icmp_seq=pack_no;icmp->icmp_id=pid;icmp->icmp data=gettickcount();icmp->icmp_cksum=cal_chksuni( (unsigned short *)icmp, packsize);/*d?nq 甌?*/return packsize;/?a?uint unpack(uns

13、igned char *buf, int len) struct ip *ip;struct icmp *icmp;double rtt;int iphdrlen;ip= (struct ip *)buf;iphdrlen=ip->ip hl*4;icmp=(struct icmp *)(buf+iphdrlen);if( (icmp->icmp_type=icmp_echoreply) && (icmp->icmp_id=pid)i:len=len-iphdrlen-8;rtt二gettickcount()-icmp->icmp data;printf

14、 (,zreply from %s: bytes=%d time=%. ofms ttl=%d icmp seq=%unzz,inet_ntoa(from_addr. sin_addr),len,rtt,ip-ipttl,icmp-icmp seq);return 1;return 0;/?elvoid send_packet()iint packetsize;static int pack no=0; packetsize=pack(pack no+);if( sendto(sockfd, (char *)sendpacket,packetsize,0, (structsockaddr *)&dest addr, sizeof (dest addr) )<0 ) printf (destination host unreachable. nz/); printf ("send no %dn,packjnot); /?6eo void recv_packet ()!int n, fromlen;int success;fromlen二sizeof (froni_addr) ; doiif( (n二recvfro

溫馨提示

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

評(píng)論

0/150

提交評(píng)論