Linux下C语言获取本机IP地址
2013-05-19 00:58
260 查看
#include <sys/ioctl.h> #include <net/if.h> #include <arpa/inet.h> char* GetLocalIp() { int MAXINTERFACES=16; char *ip = NULL; int fd, intrface, retn = 0; struct ifreq buf[MAXINTERFACES]; struct ifconf ifc; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) >= 0) { ifc.ifc_len = sizeof(buf); ifc.ifc_buf = (caddr_t)buf; if (!ioctl(fd, SIOCGIFCONF, (char *)&ifc)) { intrface = ifc.ifc_len / sizeof(struct ifreq); while (intrface-- > 0) { if (!(ioctl (fd, SIOCGIFADDR, (char *) &buf[intrface]))) { ip=(inet_ntoa(((struct sockaddr_in*)(&buf[intrface].ifr_addr))->sin_addr)); break; } } } close (fd); return ip; } }
相关文章推荐
- Linux下C语言获取和修改本机IP地址
- 获取本机的IP地址(Windows和Linux兼容)
- vxworks中c语言获取系统名及获取IP地址例子
- C语言获取本地所有网卡的ip地址及MAC信息
- linux 获取本机的所有IP地址
- C语言和汇编语言获取linux系统时间
- Linux 中获取指定网口的IP地址(c实现)
- linux C如何获取服务器节点上所有网口的ip地址
- Linux系统下用C语言获取MAC地址
- LINUX下获取IP地址和MAC地址
- Linux 获取本机IP地址和MAC地址(一)
- 用c语言得到linux本机的IP地址
- C语言实现获取LINUX当前时间
- Linux下用C语言设置和获取系统时间
- (转载)Linux上编程获取本机IP地址(而不是127.0.0.1)
- 在linux下c语言获取本机ip地址源代码
- Linux如何自动获取IP地址及配置静态IP地址上网
- linux c获取IP地址
- ARM-Linux配置DHCP自动获取IP地址
- 获取本机ip地址(适用于linux、windows)