<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Linux實(shí)用技巧 用ICMP實(shí)現(xiàn)簡(jiǎn)單的Ping功能

          Linux實(shí)用技巧 用ICMP實(shí)現(xiàn)簡(jiǎn)單的Ping功能

          作者: 時(shí)間:2016-10-08 來(lái)源:網(wǎng)絡(luò) 收藏

          如果目的主機(jī)在工 輸出在工狀態(tài) 如果5妙內(nèi)無(wú)相應(yīng) 用SIGALRM信號(hào)中斷進(jìn)程

          本文引用地址:http://www.ex-cimer.com/article/201610/305451.htm

          #include unp.h

          void send_echo_req(int sockfd, struct sockaddr_in *dstaddr);

          uint16_t in_cksum(uint16_t *addr, int len);

          void recv_echo_reply(int sockfd);

          int main(int argc, char **argv)

          {

          int sockfd;

          struct sockaddr_in dstaddr;

          if ((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1)

          err_sys(socket);

          bzero(dstaddr, sizeof(dstaddr));

          dstaddr.sin_family = AF_INET;

          dstaddr.sin_port = htons(0);

          if (inet_pton(AF_INET, argv[1], dstaddr.sin_addr) = 0)

          err_sys(inet_pton);

          send_echo_req(sockfd, dstaddr);

          recv_echo_reply(sockfd);

          exit(0);

          }

          void send_echo_req(int sockfd, struct sockaddr_in *dstaddr)

          {

          char buf[100];

          size_t len = sizeof(struct icmp);

          struct icmp *icmp;

          socklen_t dstlen = sizeof(struct sockaddr_in);

          bzero(buf, sizeof(buf));

          icmp = (struct icmp *)buf;

          icmp->icmp_type = ICMP_ECHO;

          icmp->icmp_code = 0;

          icmp->icmp_id = getpid();

          icmp->icmp_seq = 1;

          icmp->icmp_cksum = in_cksum((uint16_t *) icmp, sizeof(struct icmp));

          if (sendto(sockfd, buf, len, 0, (SA *)dstaddr, dstlen) == -1)

          err_sys(sendto);

          }

          void recv_echo_reply(int sockfd)

          {

          char buf[100];

          ssize_t n;

          struct ip *ip;

          struct icmp *icmp;

          while (1) {

          alarm(5); /* set timeout */

          if ((n = read(sockfd, buf, sizeof(buf))) == -1)

          err_sys(read);

          ip = (struct ip *)buf;

          if (ip->ip_p != IPPROTO_ICMP) {

          fprintf(stderr, protocol error.rn);

          exit(1);

          }

          icmp = (struct icmp *)(buf + sizeof(struct ip));

          if (icmp->icmp_type == ICMP_ECHOREPLY) {

          if (icmp->icmp_id != getpid()) {

          fprintf(stderr, not this process.rn);

          exit(1);

          } else {

          printf(destination host is alive.rn);

          break;

          }

          }

          }

          }

          uint16_t in_cksum(uint16_t *addr, int len)

          {

          int nleft = len;

          uint32_t sum = 0;

          uint16_t *w = addr;

          uint16_t answer = 0;

          while (nleft > 1) {

          sum += *w++;

          nleft -= 2;

          }

          if (nleft == 1) {

          *(unsigned char *)(answer) = *(unsigned char *)w ;

          sum += answer;

          }

          sum = (sum >> 16) + (sum 0xffff);

          sum += (sum >> 16);

          answer = ~sum;

          return(answer);

          }

          void err_sys(const char *errmsg)

          {

          perror(errmsg);

          exit(1);

          }



          關(guān)鍵詞:

          評(píng)論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();