int gethostname(char *name, size_t len);
获取本地主机名存入name[len],成功返回0,失败返回-1;

  struct hostent * gethostbyname(const char * hostname);  //返回对应于给定主机名的包含主机名字和地址信息的hostent结构的指针

  struct hostent
  {
    char *h_name;  //所查询主机规范名
    char **h_aliases;  //
    int h_addrtype;
    int h_length;
    char **h_addr_list;  //本机Ip地址列表, 指针的指针存放所有的IP
  };

#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<errno.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<signal.h>
#include<netdb.h>

#define ERR_EXIT(m)\
do\
{\
perror(m);\
exit(EXIT_FAILURE);\
}while(0)
//获取本机IP。
int getlocalip(char * ip,char *host)
{
struct hostent *hp;
if((hp=gethostbyname(host))==NULL)
return -1;
strcpy(ip,inet_ntoa(*(struct in_addr*)hp->h_addr_list[0])); //还有一个宏 #define hp->h_addr hp->h_addr_list[0]
return 0;
}
int main()
{
char host[100]={0};
if(gethostname(host,sizeof(host))<0)
ERR_EXIT("gethostname error");//错误返回-1
printf("hostname=%s\n",host);
struct hostent *hp;
if((hp=gethostbyname(host))==NULL)
ERR_EXIT("gethostbyname error");
int i=0;//hp->h_addr_list[i]是一个char* ,实际上是一个(struct in_addr)结构指针
while(hp->h_addr_list[i]!=NULL)
{
printf("%s\n",inet_ntoa(*(struct in_addr*)hp->h_addr_list[i]));//获取本地主机IP地址
i++;//list第一个指向本地IP char* inet_ntoa(struct in_addr in);(转换成点分十进制)
}
char ip[16]={0};
getlocalip(ip,host);//list中第一个指向本地IP,实现一个获取本机默认ip的程序。
printf("local ip=%s\n",ip);
return 0;
}

gethostname(获取主机名)、gethostbyname(由主机名获取IP地址)的更多相关文章

  1. gethostbyname() -- 用域名或主机名获取IP地址

    #include <netdb.h>    #include <sys/socket.h> struct hostent *gethostbyname(const char * ...

  2. (转)gethostbyname() -- 用域名或主机名获取IP地址

    struct hostent *gethostbyname(const char *name); 这个函数的传入值是域名或者主机名,例如"www.google.cn"等等.传出值, ...

  3. java 获取局域网中的全部主机名和IP地址

    DOS命令 命令 意义 net view 获取局域网中的全部主机名 ipconfig -all 获取本地IP,主机名,MAC地址 arp -a 获取本局域网中的全部IP地址和物理地址 ping -a ...

  4. Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析(1-4)

    Linux学习笔记(10)linux网络管理与配置之一——主机名与IP地址,DNS解析与本地hosts解析 大纲目录 0.常用linux基础网络命令 1.配置主机名 2.配置网卡信息与IP地址 3.配 ...

  5. Solaris 10主机名和IP地址步骤

    1.修改主机名: hostname newname vi /etc/hosts vi /etc/hostname.e1000g0 vi /etc/nodename init 6 #重启 --confi ...

  6. 二十二、utl_inaddr(用于取得局域网或Internet环境中的主机名和IP地址)

    1.概述 作用:用于取得局域网或Internet环境中的主机名和IP地址. 2.包的组成 1).get_host_name作用:用于取得指定IP地址所对应的主机名语法:utl_inaddr.get_h ...

  7. Asp.net MVC获取访问系统的客户端计算机的主机名和IP地址

    string HostName = string.Empty; string ip = string.Empty; string ipv4 = String.Empty; if (!string.Is ...

  8. python获取设备主机名和IP地址

    import socket def print_machine_info(): host_name = socket.gethostname() ip_address = socket.gethost ...

  9. Linux CentOS7.0 (02)修改主机名和ip地址

    一.主机名修改 1.查看命令 在CentOS中,有三种定义的主机名:静态的(static),瞬态的(transient),和灵活的(pretty). "静态"主机名也称为内核主机名 ...

随机推荐

  1. day54 Pyhton 前端JS05

    今日内容: 1.数组Array var colors = ['red','color','yellow']; 使用new 关键词对构造函数进行创建对象 var colors2 = new Array( ...

  2. golang的http库使用代理

    1.先上代码 package main import ( "crypto/tls" "flag" "fmt" "io/ioutil ...

  3. Git软件操作过程

    一.下载 Git 二.下载Git小乌龟-TortoiseGit 三.汉化-去官网下载,官网地址 https://tortoisegit.org/download/

  4. ES异常处理-NoNodeAvailableException

    1.问题描述 ES client客户端能创建,但是在用客户端操作时报:NoNodeAvailableException[None of the configured nodes are availab ...

  5. phpstorm 注解路由插件

    idea-php-annotation-plugin 设置 插件 搜索 安装 重启

  6. 2020年的UWP(2)——In Process App Service

    最早的时候App Service被定义为一种后台服务,类似于极简版的Windows Service.App Service作为Background Task在宿主UWP APP中运行,向其他UWP A ...

  7. 解析SparkStreaming和Kafka集成的两种方式

    spark streaming是基于微批处理的流式计算引擎,通常是利用spark core或者spark core与spark sql一起来处理数据.在企业实时处理架构中,通常将spark strea ...

  8. VScode如何配置c/c++运行环境

    vscode如何配置c/c++环境 下载 Mingw 参考链接:https://blog.csdn.net/jiqiren_dasheng/article/details/103775488 笔者下载 ...

  9. 实战一:建立springcloud基础项目结构

    一,,创建父工程,用于管理项目依赖版本 1,new -> project -> maven 2,修改pom.xml,这里管理了springboot,springcloud,springcl ...

  10. 想买保时捷的运维李先生学Java性能之 垃圾收集算法

    前言 从原来只知道-Xms.-Xmx是设置内存的,到现在稍微理解了一些堆内存等Java虚拟机的一些知识.明白了技术这一个东西还是得要有输入才能实践,原理与实践要相辅相成,后续把JVM的监控好好总结一下 ...