生活随笔 
收集整理的這篇文章主要介紹了
                                
Linux用ICMP协议实现简单Ping网络监测功能 
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
 
                                
                            From:?http://www.linuxidc.com/Linux/2012-05/61073.htm
 
 
 ICMP是(Internet Control Message Protocol)Internet控制報文協議。它是TCP/IP協議族的一個子協議,用于在IP主機、路由器之間傳遞控制消息。控制消息是指網絡通不通、主機是否可達、路由是否可用等網絡本身的消息。這些控制消息雖然并不傳輸用戶數據,但是對于用戶數據的傳遞起著重要的作用。
 
 ICMP協議是一種面向無連接的協議,用于傳輸出錯報告控制信息。它是一個非常重要的協議,它對于網絡安全具有極其重要的意義。
 
 折騰半天,原來ICMP也是TCP/IP其中一種協議.那么監測網絡是否ping的通,就跟TCP協議差不多了。
 
 步驟簡單歸納為:1.綁定套接字,2.發送數據包 3.接收數據包 4.解析數據包 #include?<stdio.h>? #include?<sys/socket.h>? #include?<netinet/in.h>? #include?<netinet/ip.h>? #include?<netinet/ip_icmp.h>? #include?<netdb.h> ?  
#define?PACKET_SIZE?????4096? #define?ERROR???????????0? #define?SUCCESS?????????1? ?? ?? unsigned?short short int  {?? ????int  ????int  ????unsigned?short  ????unsigned?short  ?????? ????while  ????{?? ????????sum?+=?*w++;?? ????????nleft?-=?2;?? ????}?? ?????? ????if  ????{????????? ????????*(unsigned?char char  ????????sum?+=?answer;?? ????}?? ?????? ????sum?=?(sum?>>?16)?+?(sum?&?0xffff);?? ????sum?+=?(sum?>>?16);?? ????answer?=?~sum;?? ?????? ????return  }?? ?? int char int {???????? ????struct  ????int  ????fd_set?readfds;???? ?????? ????struct  ????struct  ?????? ????bzero(&addr,sizeof  ????addr.sin_family?=?AF_INET;???? ????addr.sin_addr.s_addr?=?inet_addr(ips);???? ?????? ????int  ?????? ????sockfd?=?socket(AF_INET,?SOCK_RAW,?IPPROTO_ICMP);???? ????if  ????{???? ????????printf("ip:%s,socket?error\n"  ????????return  ????}???? ?????? ????struct  ?????? ????timeo.tv_sec?=?timeout?/?1000;???? ????timeo.tv_usec?=?timeout?%?1000;???? ?????? ????if sizeof  ????{???? ????????printf("ip:%s,setsockopt?error\n"  ????????return  ????}???? ?????? ????char  ????char  ?????? ????memset(sendpacket,?0,?sizeof  ?????? ????pid_t?pid;???? ?????? ????pid=getpid();???? ?????? ????struct  ????struct  ?????? ???? ????icmp=(struct  ????icmp->icmp_type=ICMP_ECHO;???? ????icmp->icmp_code=0;???? ????icmp->icmp_cksum=0;???? ????icmp->icmp_seq=0;???? ????icmp->icmp_id=pid;??? ????tval=?(struct  ????gettimeofday(tval,NULL);???? ????icmp->icmp_cksum=cal_chksum((unsigned?short sizeof struct  ?????? ????int  ?????? ????n?=?sendto(sockfd,?(char sizeof struct struct sizeof  ????if  ????{???? ????????printf("ip:%s,sendto?error\n"  ????????return  ????}???? ?????? ?????? ?????? ????while  ????{???? ?????????? ????????FD_ZERO(&readfds);???? ????????FD_SET(sockfd,?&readfds);???? ????????maxfds?=?sockfd?+?1;???? ????????n?=?select(maxfds,?&readfds,?NULL,?NULL,?&timeo);???? ????????if  ????????{???? ????????????printf("ip:%s,Time?out?error\n"  ????????????close(sockfd);???? ????????????return  ????????}???? ?????????? ?????????? ????????memset(recvpacket,?0,?sizeof  ????????int sizeof  ????????n?=?recvfrom(sockfd,?recvpacket,?sizeof struct  ????????if  ????????????break  ????????}???? ?????????? ??????? ????????char char  ?????????????? ????????if  ????????{???? ????????????printf("NowPingip:%s?Fromip:%s\nNowPingip?is?not?same?to?Fromip,so?ping?wrong!\n"  ????????????return  ????????}???? ?????????? ????????iph?=?(struct  ?????????? ????????icmp=(struct  ?????????? ????????printf("ip:%s\n,icmp->icmp_type:%d\n,icmp->icmp_id:%d\n"  ?????????? ????????if  ????????{???? ?????????????? ????????????break  ????????}???? ????????else  ????????{???? ?????????????? ????????????continue  ????????}???? ????}???? ?????? ????int  {?? ????char  ????printf("Please?input?ping?IP:"  ????scanf("%s"  ?????? ????if  ????{?? ????????printf("Ping?succeed!\n"  ????}?? ????else  ????{?? ????????printf("Ping?wrong!\n"  ????}?? ?????? }??   測試結果:
 
 
root@an-virtual-machine:~/wyz/test# ./testping 總結 
                            
                                以上是生活随笔 為你收集整理的Linux用ICMP协议实现简单Ping网络监测功能 的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。