http://blog.csdn.net/K346K346/article/details/48231933

int main ()
{
/*
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
char *addr; getifaddrs (&ifap);
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
if (ifa->ifa_addr->sa_family==AF_INET) {
sa = (struct sockaddr_in *) ifa->ifa_addr;
addr = inet_ntoa(sa->sin_addr);
printf("Interface: %s\tAddress: %s, %x \n", ifa->ifa_name, addr,sa->sin_addr);
}
} freeifaddrs(ifap); */

/*
//设置为本机IP,如果获取地址失败则使用hostname,这种获取方式依赖/etc/hosts,获取不准。
// 获取本机地址,只取一个
hostent* pHost = gethostbyname(host_name);
if ( pHost != 0 )
{
for ( int i = 0; pHost != 0 && pHost->h_addr_list[i] != 0; i++ )
{
if((unsigned int)((unsigned char*)pHost->h_addr_list[i])[0] == 127)
continue;
char ip_str[16] = {0};
sprintf( ip_str, "%x%x%x%x",(unsigned int)((unsigned char*)pHost->h_addr_list[i])[0],
(unsigned int)((unsigned char*)pHost->h_addr_list[i])[1],
(unsigned int)((unsigned char*)pHost->h_addr_list[i])[2],
(unsigned int)((unsigned char*)pHost->h_addr_list[i])[3]);
host_id_ = ip_str;


break;
}
}
*/

struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;

getifaddrs (&ifap);
for (ifa = ifap; ifa; ifa = ifa->ifa_next)
{
if (ifa->ifa_addr && ifa->ifa_addr->sa_family==AF_INET)
{
if(strncmp(ifa->ifa_name, "lo", 2) == 0)
continue;

sa = (struct sockaddr_in *) ifa->ifa_addr;
char ip_str[16] = {0};
sprintf(ip_str,"%08x", sa->sin_addr);
printf("---%s \n",ip_str);
printf("%d,%d,%d,%d \n",sizeof(sa->sin_addr),sizeof(int),sizeof(unsigned long), sizeof(unsigned long long));
}
}
freeifaddrs(ifap);

return 0;
}

 

Linux 下获取本机IP的更多相关文章

  1. Linux下获取本机IP地址的代码

    Linux下获取本机IP地址的代码,返回值即为互联网标准点分格式的字符串. #define ETH_NAME "eth0" //获得本机IP地址 char* GetLocalAdd ...

  2. linux下获取本机IP

    转载:http://blog.chinaunix.net/uid-20593763-id-1620213.html 源代码级Unix/Linux 通用网卡IP地址获取方法 在Unix和Linux系统下 ...

  3. Windows下获取本机IP地址方法介绍

    Windows下获取本机IP地址方法介绍 if((hostinfo = gethostbyname(name)) != NULL) { #if 1 ; printf("IP COUNT: % ...

  4. Linux下获得本机IP(非127.0.0.1)

    在Linux下用InetAddress.getLocalHost()方法获取本机IP地址,得到的结果总是:127.0.1.1.原来这个是etc/hosts文件中的配置,并非网卡的IP地址. 可用代码如 ...

  5. QT5下获取本机IP地址、计算机名、网络连接名、MAC地址、子网掩码、广播地址

    获取主机名称 /* * 名称:get_localmachine_name * 功能:获取本机机器名称 * 参数:no * 返回:QString */ QString CafesClient::get_ ...

  6. Linux下获取和设置IP

    在Linux下获取关于IP和网关的操作:重点是对struct ifreq 的操作. 那么进入目录/usr/include/net/if.h下看查找struct ifreq结构体. /* Interfa ...

  7. rust下获取本机IP

    又拾起了rust语言, 想写一点东西玩一玩, 但是发现连一个获取本机IP地址的库都没有, 还得挽起袖子自己撸. https://crates.io/crates/local_ipaddress 没有用 ...

  8. python未知网卡名情况下获取本机IP

    import socket def get_ip(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: # doesn't even ...

  9. windows和linux下的本机IP的获取(亲测有效)

    package com.handsight.platform.fras.util; import org.apache.log4j.Logger; import javax.servlet.http. ...

随机推荐

  1. 牛客网多校训练第三场 A - PACM Team(01背包变形 + 记录方案)

    链接: https://www.nowcoder.com/acm/contest/141/A 题意: 有n(1≤n≤36)个物品,每个物品有四种代价pi,ai,ci,mi,价值为gi(0≤pi,ai, ...

  2. UVa 1625 - Color Length(线性DP + 滚动数组)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  3. HDU 6464 免费送气球 【权值线段树】(广东工业大学第十四届程序设计竞赛)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6464 免费送气球 Time Limit: 2000/1000 MS (Java/Others)    M ...

  4. 3springboot:springboot配置文件(外部配置加载顺序、自动配置原理,@Conditional)

    1.外部配置加载顺序 SpringBoot也可以从以下位置加载配置: 优先级从高到低 高优先级的配置覆盖低优先级的配置,所有的配置会形成互补配置  1.命令行参数 所有的配置都可以在命令行上进行指定 ...

  5. MSF入侵安卓手机

    MSF是Metasploit的简称,Metasploit是一款开源的安全漏洞检测工具,非常强大,分别有Windows版和Linux版,工具里集成了许多微软公布的漏洞(0day). 我这里做测试的系统是 ...

  6. CToolBarCtrl基本内容控件

    基本内容CToolBarCtrl CObject └CCmdTarget └CWnd └CToolBarCtrl CToolBarCtrl类提供了Windows工具条通用控件的性能.这个控件(也就是C ...

  7. HUD 1288 Hat's Tea(反向的贪心,非常好的一道题)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1288 Hat's Tea Time Limit: 2000/1000 MS (Java/Others)  ...

  8. [node.js学习]为node.js写的一个操作mysql的类

    不支持一个对象在不同异步中使用,模仿的php框架 speedphp中的model模块 GaryMysql.js var mysql = require('mysql'); var pool = nul ...

  9. careercup-扩展性和存储限制10.6

    题目 你有10亿个url,每个url对应一个非常大的网页.你怎么检测重复的网页? 解答 网页大,数量多,要把它们载入内存是不现实的. 因此我们需要一个更简短的方式来表示这些网页.而hash表正是干这事 ...

  10. 再探motan

    前言:上周使用motan是通过group远程调用超级土豆的服务,但是因为我需要写一些服务,不得不在本地启动服务,于是就详细的自己配置了一次motan. 上一篇博客也说到了,motan主要有3部分组成: ...