首先介绍一下用到的结构体

struct hostent {
  const char *h_name; // official name of host
  char **h_aliases; // alias list
  short h_addrtype; // host address type
  short h_length; // length of address
  char **h_addr_list; // list of addresses from name server
  #define h_addr h_addr_list[0] // address, for backward compatiblity
};

struct in_addr {

  in_addr_t s_addr;
};

这里是这个数据结构的详细资料:
struct hostent:
  h_name – 地址的正式名称。
  h_aliases – 空字节-地址的预备名称的指针。
  h_addrtype – 地址类型; 通常是AF_INET。
  h_length – 地址的比特长度。
  h_addr_list – 零字节-主机网络地址指针,网络字节顺序。
  h_addr - h_addr_list中的第一地址。
gethostbyname() 成功时返回一个指向结构体 hostent 的指针,或者 是个空 (NULL) 指针, 完整代码如下:

#include <stdio.h>
#include <unistd.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

int main()
{
  char name[65];
  struct hostent* pHost;
  struct hostent hs;
  int i, j;
  int cnt = 0;
  char *ips[8];

  gethostname(name, sizeof(name));
  printf("hostname = %s\n", name);

  /* 获取本机地址 */
  pHost = &hs;
  pHost = gethostbyname(name);

  printf("%s\n",pHost->h_name);
  printf("%d\n",pHost->h_addr);
  struct in_addr *in= (struct in_addr *)pHost->h_addr;
  printf("%s\n", inet_ntoa(*in));

  for (i = 0; pHost != 0 && pHost->h_addr_list[i] != 0; i++) {
    char ip[16]; ip[0] = 0;
    for (j = 0; j < pHost->h_length; j++) {
      char tmp[16];
      if(j > 0) {
        strcat(ip, ".");
      }
      sprintf( tmp, "%u",
          (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
        strcat(ip, tmp);
    }
    ips[cnt] = (char*)malloc(64);
    strcpy(ips[cnt++], ip);
  }

  return 0;
}

怎样获取本机的ip地址的更多相关文章

  1. 【Win 10 应用开发】获取本机的IP地址

    按照老规矩,也是朋友的建议,老周今天在吹牛之前,先讲一个小故事. 有朋友问我,老周,你现在还发短信吗,你每个月用多少电话费?唉,实话说,现在真的发短信不多了,套餐送的130条短信,每月都发不了一条.至 ...

  2. 获取本机的IP地址(局域网)与主机名称

    编写内容保存为bat @echo off &setlocal enabledelayedexpansion Rem '/*========获取本机的IP地址(局域网)=========*/ e ...

  3. 获取本机的IP地址和mac地址

    1. 以前一直用ipconfig来查看ip地址,哈哈哈,现在发现挺好玩 #获取本机的IP地址和mac地址 import uuid import socket def get_mac_address() ...

  4. Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP

    Java 工具类 IpUtil - 获取本机所有 IP 地址,LocalHost 对应地址 IP IP 工具类 源代码: /** * <p> * * @author XiaoPengwei ...

  5. 获取本机的ip地址(排除虚拟机,蓝牙等ip)

    项目中遇到了要获取本地ip的需求,网上查找资料遇到很多坑,很多Java获取本机ip地址的方法要么是根本获取不到,要么是获取的有问题. 网上常见的方法如下 InetAddress.getLocalHos ...

  6. C++获取本机的ip地址程序

    #include <WinSock2.h> #pragma comment(lib,"ws2_32") //链接到ws2_32动态链接库 class CInitSock ...

  7. android获取本机的IP地址和mac物理地址

    /获取本机IP地址 public String getLocalIpAddress() { WifiManager wifiManager = (WifiManager) getSystemServi ...

  8. C#获取本机IP方法,获取本机局域网IP地址方法

    1. private void GetIP() { string hostName = Dns.GetHostName();//本机名 //System.Net.IPAddress[] address ...

  9. 获取本机的IP地址

    /// <summary> /// 获取本机IP地址 /// </summary> /// <returns>本机IP地址</returns> publ ...

随机推荐

  1. 微信nickname乱码及mysql编码格式设置(utf8mb4)

    微信nickname乱码及mysql编码格式设置(utf8mb4) 今天在写微信公众平台项目时,写到一个用户管理模块,接口神马的已经调试好了,于是将用户从微信服务器保存到本地数据库,发现报错: jav ...

  2. 脚本工具: 查看当前系统被写入的FD

    #!/bin/bash touch /tmp/sn2 /tmp/sn4 /tmp/sn6 /tmp/sn3 redir=/dev/null which lsof >&/dev/null ...

  3. C#遍历集合与移除元素的方法

    如果用foreach,会造成被遍历的集合更改后带来异常问题. 此时,用for循环可有效的解决这个问题. for(int i=0;i<List.Count;i++) { if(条件是真) { Li ...

  4. NHibernate系列文章二十一:延迟加载

    摘要 NHibernate的延迟加载机制是很重要的内容.通过关系映射将数据库表之间的关系映射成对象之间的关系,如果没有延迟加载机制,从主表的一个对象的查询将直接查询出所有与该对象关联的其他对象,如果关 ...

  5. ReadReadMe

    ∮博客说明 §标题格式说明(只在这篇文档中说明一次) 此站博客分两类 普通博客,补丁博客 普通博客标题格式为 NumberType_Title     Number: 博客编号 ,按时间编号,编号只在 ...

  6. HTTP服务&Ajax编程知识点导图

  7. bootstrap源码分析----栅格系统

    Bootstrap 提供了一套响应式.移动设备优先的流式栅格系统,随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列. bootstrap通过媒体查询解决不同分辨率屏幕下,页面主内 ...

  8. excel解析二维数组结构的excel

    public void fileImport(Ufile ufile) throws Exception { String filePath = ufile.getFilePath(); List&l ...

  9. elasticsearch同义词及动态更新

    第一种:参考地址:http://dev.paperlesspost.com/setting-up-elasticsearch-synonyms/271.Add a synonyms file.2.Cr ...

  10. php获取时间问题,用默认配置读到本地时间。。。。。

    用date获取到时间有8小时时差 因为php用date获取到的是格林威治时区的时间,而大陆时间和格林威治时间有8个小时时差,所以.... 修改:网上有各种修改方式: 比如:在程序中添加时间的初始化的语 ...