linux编程获取本机IP地址的三种方法
来源:互联网 发布:电脑垃圾清理软件 编辑:程序博客网 时间:2024/05/22 12:00
http://blog.csdn.net/jcwkyl/article/details/6324147
这 是一项不太清晰而且没有多大意义的工作。一个原因是网络地址的设置非常灵活而且都是允许用户进行个性化设置的,比如一台计算机上可以有多块物理网卡或者虚 拟网卡,一个网卡上可以绑定多个IP地址,用户可以为网卡设置别名,可以重命名网卡,用户计算机所在网络拓扑结构未知,主机名设置是一个可选项并且同样可 以为一个计算机绑定多个主机名等,这些信息都会有影响。脱离了网络连接,单独的网络地址没有任何意义。编程中遇到必须获取计算机IP的场景,应该考虑将这 一选项放到配置文件中,由用户自己来选择。
通过google,编程获取IP地址大约有以下三种思路:
1. 通过gethostname()和gethostbyname()
#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main() {
char hname[128];
struct hostent *hent;
int i;
gethostname(hname, sizeof(hname));
//hent = gethostent();
hent = gethostbyname(hname);
printf("hostname: %s/naddress list: ", hent->h_name);
for(i = 0; hent->h_addr_list[i]; i++) {
printf("%s/t", inet_ntoa(*(struct in_addr*)(hent->h_addr_list[i])));
}
return 0;
}
运行:
[whb@jcwkyl c]$ ./local_ip
hostname: jcwkyl.jlu.edu.cn
address list: 10.60.56.90
2. 通过枚举网卡,API接口可查看man 7 netdevice
/*代码来自StackOverflow: http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-computer */
#include <stdio.h>
#include <sys/types.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
int main (int argc, const char * argv[]) {
struct ifaddrs * ifAddrStruct=NULL;
void * tmpAddrPtr=NULL;
getifaddrs(&ifAddrStruct);
while (ifAddrStruct!=NULL) {
if (ifAddrStruct->ifa_addr->sa_family==AF_INET) { // check it is IP4
// is a valid IP4 Address
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
char addressBuffer[INET_ADDRSTRLEN];
inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
printf("%s IP Address %s/n", ifAddrStruct->ifa_name, addressBuffer);
} else if (ifAddrStruct->ifa_addr->sa_family==AF_INET6) { // check it is IP6
// is a valid IP6 Address
tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
char addressBuffer[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
printf("%s IP Address %s/n", ifAddrStruct->ifa_name, addressBuffer);
}
ifAddrStruct=ifAddrStruct->ifa_next;
}
return 0;
}
运行 :
[whb@jcwkyl c]$ ./local_ip2
lo IP Address 127.0.0.1
eth0 IP Address 10.60.56.90
eth0:1 IP Address 192.168.1.3
lo IP Address ::
eth0 IP Address ::2001:da8:b000:6213:20f:1fff
eth0 IP Address 0:0:fe80::20f:1fff
3. 打开一个对外界服务器的网络连接,通过getsockname()反查自己的IP
- linux编程获取本机IP地址的三种方法
- linux编程获取本机IP地址的三种方法
- linux编程获取本机IP地址的三种方法
- linux编程获取本机IP地址的三种方法
- linux编程获取本机IP地址的三种方法
- linux编程获取本机IP地址
- linux编程获取本机IP地址
- linux编程获取本机IP地址
- Linux编程获取本机IP地址
- linux编程获取本机IP地址
- linux编程获取本机IP地址
- linux编程获取本机IP地址
- linux获取本机IP地址的方法 struct ifconf
- Linux 获取本机MAC,IP地址的方法
- 获取本机IP地址的方法:
- 两种获取本机IP地址的方法
- c# 获取本机IP地址的两种方法
- window上获取本机IP地址方法--windows编程
- 向数据库中批量录入数据
- access注入思路
- Linux-2.6.32.2内核在mini2440上的移植(九)---触摸屏驱动移植
- Linux-2.6.32.2内核在mini2440上的移植(十)---配置USB外设
- Linux内核分析之进程总结
- linux编程获取本机IP地址的三种方法
- request和session以及application辨析(并用application实现简单的网页计数器)
- 设计模式——创建型模式
- 程序员的本质
- .Linux-2.6.32.2内核在mini2440上的移植(十一)---移植SD卡驱动
- git工具的使用
- Linux-2.6.32.2内核在mini2440上的移植(十二)---移植I2C EEPROM驱动
- js event.x,event.clientX,event.offsetX区别
- 《编程之美》2.18——数组分割新思路(包含分类后数组的输出)