Chinaunix首页 | 论坛 | 博客
  • 博客访问: 355208
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: LINUX

2011-05-09 15:02:49

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/socket.h>
  5. #include <net/if.h>
  6. #include <netinet/in.h>

  7. int main()
  8. {
  9.     int i=0;
  10.     int sockfd;

  11.       struct ifconf ifconf;
  12.       unsigned char buf[512];
  13.       struct ifreq *ifreq;

  14.       //初始化ifconf

  15.       ifconf.ifc_len = 512;
  16.       ifconf.ifc_buf = buf;

  17.       if((sockfd = socket(AF_INET, SOCK_DGRAM, 0))<0)
  18.       {
  19.           perror("socket");
  20.         exit(1);
  21.     }

  22.       ioctl(sockfd, SIOCGIFCONF, &ifconf); //获取所有接口信息

  23.   //接下来一个一个的获取IP地址

  24.       ifreq = (struct ifreq*)buf;

  25.       for(i=(ifconf.ifc_len/sizeof(struct ifreq)); i>0; i--)
  26.     {
  27.         printf("name = [%s]\n", ifreq->ifr_name);
  28.           printf("local addr = [%s]\n",
  29.         inet_ntoa(((struct sockaddr_in*)&(ifreq->ifr_addr))->sin_addr));
  30.           ifreq++;
  31.       }

  32.     return 0;

  33. }
阅读(695) | 评论(0) | 转发(0) |
0

上一篇:如果Amarok无法播放

下一篇:我好累

给主人留下些什么吧!~~