- 試題排行
- 最新熱點(diǎn)
- 最新推薦
2
3
4
5
6
7
8
9
10
2008年上半年軟考軟件設(shè)計(jì)師考試試題(上午)
2008年上半年軟考網(wǎng)絡(luò)工程師考試試題(下午)
2008年上半年軟考軟件設(shè)計(jì)師考試試題(下午)
2008年上半年軟件水平考試程序員考試試題(上
2008年下半年軟考網(wǎng)絡(luò)工程師預(yù)測(cè)試題及答案
2008年上半年軟件水平考試程序員考試試題(下
2008下半年軟件水平考試軟件設(shè)計(jì)師押題試卷
08年上半年軟考數(shù)據(jù)庫(kù)系統(tǒng)工程師考試試題(上
2008下半年軟件水平考試程序員模擬試題及答
ping的原理就是首先建立通道,然后發(fā)送包,對(duì)方接受后返回信息,這個(gè)包至少包括以下內(nèi)容,發(fā)送的時(shí)候,包的內(nèi)容包括對(duì)方的ip地址和自己的地址,還有序列數(shù),回送的時(shí)候包括雙方地址,還有時(shí)間等,主要是接受方在都是在操作系統(tǒng)內(nèi)核里做好的,時(shí)刻在監(jiān)聽(tīng),提供一段c程序的代碼,希望對(duì)大家有用。
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd,datalen=56;
int nsend=0,nreceived=0;
struct sockaddr_in dest_addr;
pid_t pid;
struct sockaddr_in from;
struct timeval tvrecv;
void statistics(int signo);
unsigned short cal_chksum(unsigned short *addr,int len);
int pack(int pack_no);
void send_packet(void);
void recv_packet(void);
int unpack(char *buf,int len);
void tv_sub(struct timeval *out,struct timeval *in);
void statistics(int signo)
{ printf("\n--------------------PING statistics-------------------\n");
printf("%d packets transmitted, %d received , %%%d lost\n",nsend,nreceived,
(nsend-nreceived)/nsend*100);
close(sockfd);
exit(1);
}
/*校驗(yàn)和算法*/
unsigned short cal_chksum(unsigned short *addr,int len)
{ int nleft=len;
int sum=0;
unsigned short *w=addr;
unsigned short answer=0;
/*把ICMP報(bào)頭二進(jìn)制數(shù)據(jù)以2字節(jié)為單位累加起來(lái)*/
while(nleft>1)
{ sum+=*w++;
nleft-=2;
}
/*若ICMP報(bào)頭為奇數(shù)個(gè)字節(jié),會(huì)剩下最后一字節(jié)。把最后一個(gè)字節(jié)視為一個(gè)2字節(jié)數(shù)據(jù)的高字節(jié),這個(gè)2字節(jié)數(shù)據(jù)的低字節(jié)為0,繼續(xù)累加*/
if( nleft==1)
{ *(unsigned char *)(&answer)=*(unsigned char *)w;
sum+=answer;
}
sum=(sum>>16)+(sum&0xffff);
sum+=(sum>>16);
answer=~sum;
return answer;
}
/*設(shè)置ICMP報(bào)頭*/
int pack(int pack_no)
{ int i,packsize;
struct icmp *icmp;
struct timeval *tval;
icmp=(struct icmp*)sendpacket;
icmp->icmp_type=ICMP_ECHO;
icmp->icmp_code=0;
icmp->icmp_cksum=0;
icmp->icmp_seq=pack_no;
icmp->icmp_id=pid;
packsize=8+datalen;
tval= (struct timeval *)icmp->icmp_data;
gettimeofday(tval,NULL); /*記錄發(fā)送時(shí)間*/
icmp->icmp_cksum=cal_chksum( (unsigned short *)icmp,packsize); /*校驗(yàn)算法*/
return packsize;
}
/*發(fā)送三個(gè)ICMP報(bào)文*/
void send_packet()
{ int packetsize;
while( nsend
packetsize=pack(nsend); /*設(shè)置ICMP報(bào)頭*/
if( sendto(sockfd,sendpacket,packetsize,0,
(struct sockaddr *)&dest_addr,sizeof(dest_addr) )<0 )
{ perror("sendto error");
continue;
}
sleep(1); /*每隔一秒發(fā)送一個(gè)ICMP報(bào)文*/
}
}
/*接收所有ICMP報(bào)文*/
void recv_packet()
{ int n,fromlen;
extern int errno;
signal(SIGALRM,statistics);
fromlen=sizeof(from);
while( nreceived
if( (n=recvfrom(sockfd,recvpacket,sizeof(recvpacket),0,
(struct sockaddr *)&from,&fromlen)) <0)
{ if(errno==EINTR)continue;
perror("recvfrom error");
continue;
}
gettimeofday(&tvrecv,NULL); /*記錄接收時(shí)間*/
if(unpack(recvpacket,n)==-1)continue;
nreceived++;
}
}
/*剝?nèi)CMP報(bào)頭*/
int unpack(char *buf,int len)
{ int i,iphdrlen;
struct ip *ip;
struct icmp *icmp;
struct timeval *tvsend;
double rtt;
ip=(struct ip *)buf;
iphdrlen=ip->ip_hl<<2; /*求ip報(bào)頭長(zhǎng)度,即ip報(bào)頭的長(zhǎng)度標(biāo)志乘4*/
icmp=(struct icmp *)(buf+iphdrlen); /*越過(guò)ip報(bào)頭,指向ICMP報(bào)頭*/
len-=iphdrlen; /*ICMP報(bào)頭及ICMP數(shù)據(jù)報(bào)的總長(zhǎng)度*/
if( len<8) /*小于ICMP報(bào)頭長(zhǎng)度則不合理*/
{ printf("ICMP packets\'s length is less than 8\n");
return -1;
}
/*確保所接收的是我所發(fā)的的ICMP的回應(yīng)*/
if( (icmp->icmp_type==ICMP_ECHOREPLY) && (icmp->icmp_id==pid) )
{ tvsend=(struct timeval *)icmp->icmp_data;
tv_sub(&tvrecv,tvsend); /*接收和發(fā)送的時(shí)間差*/
rtt=tvrecv.tv_sec*1000+tvrecv.tv_usec/1000; /*以毫秒為單位計(jì)算rtt*/
/*顯示相關(guān)信息*/
printf("%d byte from %s: icmp_seq=%u ttl=%d rtt=%.3f ms\n",
len,
inet_ntoa(from.sin_addr),
icmp->icmp_seq,
ip->ip_ttl,
rtt);
}
else return -1;
}
main(int argc,char *argv[])
{ struct hostent *host;
struct protoent *protocol;
unsigned long inaddr=0l;
int waittime=MAX_WAIT_TIME;
int size=50*1024;
if(argc<2)
{ printf("usage:%s hostname/IP address\n",argv[0]);
exit(1);
}
if( (protocol=getprotobyname("icmp") )==NULL)
{ perror("getprotobyname");
exit(1);
}
/*生成使用ICMP的原始套接字,這種套接字只有root才能生成*/
if( (sockfd=socket(AF_INET,SOCK_RAW,protocol->p_proto) )<0)
{ perror("socket error");
exit(1);
}
/* 回收root權(quán)限,設(shè)置當(dāng)前用戶權(quán)限*/
setuid(getuid());
/*擴(kuò)大套接字接收緩沖區(qū)到50K這樣做主要為了減小接收緩沖區(qū)溢出的
的可能性,若無(wú)意中ping一個(gè)廣播地址或多播地址,將會(huì)引來(lái)大量應(yīng)答*/
setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size) );
bzero(&dest_addr,sizeof(dest_addr));
dest_addr.sin_family=AF_INET;
/*判斷是主機(jī)名還是ip地址*/
if( inaddr=inet_addr(argv[1])==INADDR_NONE)
{ if((host=gethostbyname(argv[1]) )==NULL) /*是主機(jī)名*/
{ perror("gethostbyname error");
exit(1);
}
memcpy( (char *)&dest_addr.sin_addr,host->h_addr,host->h_length);
}
else /*是ip地址*/
memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length);
/*獲取main的進(jìn)程id,用于設(shè)置ICMP的標(biāo)志符*/
pid=getpid();
printf("PING %s(%s): %d bytes data in ICMP packets.\n",argv[1],
inet_ntoa(dest_addr.sin_addr),datalen);
send_packet(); /*發(fā)送所有ICMP報(bào)文*/
recv_packet(); /*接收所有ICMP報(bào)文*/
statistics(SIGALRM); /*進(jìn)行統(tǒng)計(jì)*/
return 0;
}
/*兩個(gè)timeval結(jié)構(gòu)相減*/
void tv_sub(struct timeval *out,struct timeval *in)
{ if( (out->tv_usec-=in->tv_usec)<0)
{ --out->tv_sec;
out->tv_usec+=1000000;
}
out->tv_sec-=in->tv_sec;
}
- 推薦給朋友
- 收藏此頁(yè)
·軟考經(jīng)驗(yàn)分享:2008年網(wǎng)頁(yè)設(shè)計(jì)十條經(jīng)驗(yàn)總結(jié) (2008-10-28 15:33:37)
·不為系分而系分 談如何過(guò)系分及考過(guò)后的思考 (2007-11-27 15:59:28)
·《系統(tǒng)分析師教程》主編侯炳輝談如何進(jìn)階系分 (2007-11-14 9:26:46)
·我對(duì)2007下半年軟考網(wǎng)工題目的一點(diǎn)分析 (2007-11-10 10:43:45)
·軟考經(jīng)驗(yàn)談:軟考考前戰(zhàn)略---深挖洞廣積糧 (2007-5-22 18:07:13)
如果軟件水平考試網(wǎng)所轉(zhuǎn)載內(nèi)容不慎侵犯了您的權(quán)益,請(qǐng)與我們聯(lián)系,我們將會(huì)及時(shí)處理。如轉(zhuǎn)載本軟件水平考試網(wǎng)內(nèi)容,請(qǐng)注明出處。