Chinaunix首页 | 论坛 | 博客
  • 博客访问: 67628
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2017-05-17 17:55
文章分类
文章存档

2017年(25)

我的朋友

分类: C/C++

2017-05-18 12:00:53

在嵌入式系统中,使用goahead的时候, 以下代码是获取不到正确的ip地址的:

  1. if (gethostname(host, sizeof(host)) < 0) {
  2.     error(E_L, E_LOG, T("Can't get hostname"));
  3.     return -1;
  4. }
  5. if ((hp = gethostbyname(host)) == NULL) {
  6.     error(E_L, E_LOG, T("Can't get host address"));
  7.     return -1;
  8. }
  9. memcpy((char *) &intaddr, (char *) hp->h_addr_list[0],
  10.         (size_t) hp->h_length);

可以结合以下两个接口函数去获取需要的ip地址:

  1. #include <sys/socket.h>
  2. #include <sys/ioctl.h>
  3. #include <arpa/inet.h>
  4. #include <netinet/in.h>
  5. #include <net/if.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <stdio.h>


  9. static int getIfaceName(char *iface_name, int len)
  10. {
  11.     int r = -1;
  12.     int flgs, ref, use, metric, mtu, win, ir;
  13.     unsigned long int d, g, m;    
  14.     char devname[20];
  15.     FILE *fp = NULL;

  16.     if((fp = fopen("/proc/net/route", "r")) == NULL) {
  17.         perror("fopen error!\n");
  18.         return -1;
  19.     }

  20.     if (fscanf(fp, "%*[^\n]\n") < 0) {
  21.         fclose(fp);
  22.         return -1;
  23.     }

  24.     while (1) {
  25.         r = fscanf(fp, "%19s%lx%lx%X%d%d%d%lx%d%d%d\n",
  26.                  devname, &d, &g, &flgs, &ref, &use,
  27.                  &metric, &m, &mtu, &win, &ir);
  28.         if (r != 11) {
  29.             if ((r < 0) && feof(fp)) {
  30.                 break;
  31.             }
  32.             continue;
  33.         }

  34.         strncpy(iface_name, devname, len);
  35.         fclose(fp);
  36.         return 0;
  37.     }

  38.     fclose(fp);

  39.     return -1;
  40. }

  41. static int getIpAddress(char *iface_name, char *ip_addr, int len)
  42. {
  43.     int sockfd = -1;
  44.     struct ifreq ifr;
  45.     struct sockaddr_in *addr = NULL;

  46.     memset(&ifr, 0, sizeof(struct ifreq));
  47.     strcpy(ifr.ifr_name, iface_name);
  48.     addr = (struct sockaddr_in *)&ifr.ifr_addr;
  49.     addr->sin_family = AF_INET;

  50.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  51.         perror("create socket error!\n");
  52.         return -1;
  53.     }
  54.     
  55.     if (ioctl(sockfd, SIOCGIFADDR, &ifr) == 0) {
  56.         strncpy(ip_addr, inet_ntoa(addr->sin_addr), len);
  57.         close(sockfd);
  58.         return 0;
  59.     }

  60.     close(sockfd);

  61.     return -1;
  62. }

  63. int main(int argc, char** argv)
  64. {
  65.     struct in_addr    intaddr;
  66.     char iface_name[20];

  67.     if(getIfaceName(iface_name, sizeof(iface_name)) < 0) {
  68.         printf("get interface name error!\n");
  69.         return -1;
  70.     }

  71.     if(getIpAddress(iface_name, (char *) &intaddr, 15) < 0) {
  72.         printf("get interface ip address error!\n");
  73.         return -1;
  74.     }
  75.     
  76.     printf("address:%s\n",(char *) &intaddr);

  77.     return 0;
  78. }
阅读(1509) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~