网络编程里经常需要获得主机的相关信息,下面围绕相关的函数以及用到的结构来说明。

获得主机名:
int gethostname(
char FAR
*name, //[out] Pointer to a buffer that receives the local host name.
int namelen //[in] Length of the buffer.
);
返回0表示成功,失败返回SOCKET_ERROR,错误代码通过调用WSAGetLastError查看 根据主机名获得主机信息:
struct hostent FAR *gethostbyname(
const char FAR *name
);
该函数返回一个hostent指针,"hostent"是"host entry"的缩写,接下来看一下hostent的结构:
struct hostent {
char FAR * h_name; /* official name of host */ //主机名
char FAR * FAR * h_aliases; /* alias list */ //别名,一般为NULL
short h_addrtype; /* host address type */ //地址类型 一般为2,即AF_INET
short h_length; /* length of address */ //地址长度,ipv4地址为4
char FAR * FAR * h_addr_list; /* list of addresses */ //地址列表,假如有虚拟机的话,可能包含多个地址
#define h_addr h_addr_list[0] /* address, for backward compat */
};
举例说明:

   WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData); char szHostName[MAX_PATH] = { 0 };
gethostname(szHostName, MAX_PATH);
int err = GetLastError(); hostent *ptent = gethostbyname(szHostName);
printf("%s \t %s \t %d \n", ptent->h_name, ptent->h_aliases, ptent->h_addrtype);
int i = 0;
while(ptent->h_addr_list[i])
{ //一般机器上没有虚拟机,或者只哟一块儿网卡的话,这里只输出一项,即 h_addr_list[0] printf("addr:%s \n",inet_ntoa(*(in_addr*)ptent->h_addr_list[i]));
i ++;
} WSACleanup();
不过gethostbyname已经被遗弃使用了,推荐使用getaddrinfo。看一下原著:

The gethostbyname function retrieves host information corresponding to a host name from a host database.

Note  The gethostbyname function has been deprecated by the introduction of the getaddrinfo function. Developers creating Windows Sockets 2 applications are urged to use the getaddrinfo function instead of gethostbyname.

gethostbyname只能解决传入的主机名,当不知道主机名,只知道IP地址时,此函数就无能为力了,这就得用两外一个函数:
gethostbyaddr。该函数同样返回hostent *先看一下该函数定义:
struct HOSTENT FAR * gethostbyaddr(
const char FAR *addr, //[in] Pointer to an address in network byte order.
 int len, //[in] Length of the address.
 int type //[in] Type of the address, such as the AF_INET address family type (defined
// as TCP, UDP, and other associated Internet protocols). Address family types and
// their corresponding values are defined in the winsock2.h header file.
);

此函数的第一个参数是一个网络序的地址形式,所以要想传入第一个参数,还得调用inet_addr(),将点分十进制的地址变为网络地址
//The inet_addr function converts a string containing an (Ipv4) Internet
Protocol dotted address into a proper address for the IN_ADDRstructure.
inet_addr原型:
unsigned long inet_addr( const char FAR *cp );
举例说明:

    WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData); unsigned long ulong = inet_addr("127.0.0.1");
hostent *P = gethostbyaddr((char *)&ulong, 4, AF_INET);
printf("----------------------------------------------------\n");
int i = 0;
printf("%s \t %s \t %d \n", P->h_name, P->h_aliases, P->h_addrtype);
while(P->h_addr_list[i])
{
printf("addr :%s \n", inet_ntoa(*(in_addr *)(P->h_addr_list[i])));
i++;
} WSACleanup(); 不过有个前提,在使用以上任何函数之前先应该包含 #include <winsock2.h> 以及相应的lib #pragma comment (lib, "ws2_32.lib")

gethostname gethostbyname gethostbyaddr 获得主机相关信息的更多相关文章

  1. 使用gethostname()函数和gethostbyname()函数获取主机相关信息

    gethostname() : 返回本地主机的标准主机名. 原型如下: #include <unistd.h> int gethostname(char *name, size_t len ...

  2. 总结描述用户和组管理类命令的使用方法,系统用户相关信息,取出主机IP地址

    1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可. [root@db146 ~]# who|cut -f1 -d' ' |sort -u root 2.取出最后 ...

  3. C#获取当前主机硬件信息

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. php http头设置相关信息

    HTTP 状态码 状态码用来告诉HTTP客户端,HTTP服务器是否产生了预期的Response. HTTP/1.1中定义了5类状态码, 状态码由三位数字组成,第一个数字定义了响应的类别 1XX 提示信 ...

  5. linux lsmod命令 及相关信息

    lsmod  (list modules) 语法:lsmod 功能: lsmod命令:是一个小程序,用来显示文件.proc/modules的信息,也就是显示当前内核模块装载的模块. 补充说明: 执行l ...

  6. 使用C语言获取linux系统相关信息

    最近在写shell的时候,涉及到了获取环境变量参数和本地计算机相关信息,包括计算机设备名,用户名的信息,在这里简单总结一下.获取环境变量各项参数,可以直接使用getenv函数.man中关于getenv ...

  7. windows主机网络信息获取程序设计

    掌握windows系统获取网络信息的各种API函数的功能与调用方法,掌握设计程序显示获取到相关网络信息的方法,掌握网络字节数据与主机字节数据之间的转换.掌握这些API函数调用的错误处理方法. 利用本地 ...

  8. [linux]netstat命令详解-显示linux中各种网络相关信息

    1.功能与说明 netstat 用于显示linux中各种网络相关信息.如网络链接 路由表  接口状态链接 多播成员等等. 2.参数含义介绍 -a (all)显示所有选项,默认不显示LISTEN相关-t ...

  9. last 列出登入系统的用户相关信息

    Linux last 命令介绍 功能说明:列出目前与过去登入系统的用户相关信息. 语法:    last [-adRx][-f <记录文件>][-n <显示列数>][帐号名称. ...

随机推荐

  1. String = ""和String = null的区别

    在这要先说明下 String str;和String str=null;的关系,代码上来说两者几乎不差什么,但是这里String str=null;多了个指向空的引用 而String str ;却只是 ...

  2. Batch pk Shell - WindowsBatch与LinuxShell比较 [变量符号和关键字]

    原文地址:WindowsBatch与LinuxShell比较[变量符号和关键字] 一 简单实例1)batch file @echo off rem output helloworld ::  outp ...

  3. Windows netsh

    用法: netsh [-a AliasFile] [-c Context] [-r RemoteMachine] [-u [DomainName\]UserName] [-p Password | * ...

  4. java反射获取和设置实体类的属性值 递归所有父类

    最近做一个通用数据操作接口,需要动态获取和设置实体类的属性值,为了通用实体做了多重继承,开始网上找到代码都不支持父类操作,只能自己搞一个工具类了,此工具类可以设置和获取所有父类属性,代码贴下面拿走不谢 ...

  5. NOI2014

    听说14,15年的题是最简单的,然后除了提答以外的不那么水的题都是以前讲过.做过的,都比较好想到,但是我实现起来却有各种Bug,也完全不能在5h里AC...太弱了 [NOI2014]起床困难综合症 纯 ...

  6. shell启停服务脚本模板

    一. 启动脚本模板:符合幂等性 如果该服务已经启动,再次调用该脚本,不会报错,也就是说可以反复多次调用,另外启动成功返回 一个参数,提供给自动发布平台校验该服务是否启动 #!/bin/bash ins ...

  7. 微信小程序 tabBar模板

    tabBar导航栏 小程序tabBar,我们可以通过app.json进行配置,可以放置于顶部或者底部,用于不同功能页面的切换,挺好的... 但,,,貌似不能让动态修改tabBar(需求:通过switc ...

  8. 2019 Multi-University Training Contest 7 Kejin Player Final Exam

    Kejin Player 期望DP 题意: 初始等级为1,每一级有四个参数 r , s , x , a . 每一级有一个概率p=r/s花费a的代价升级到下一级,失败可能会倒退到x级 设从 l 到 r ...

  9. C# 中的三个高级参数 params

    params params 关键字可以指定采用数目可变的参数的方法参数. 可以发送参数声明中所指定类型的逗号分隔的参数列表或指定类型的参数数组. 还可以不发送参数. 在方法声明中的 params 关键 ...

  10. selenium基础(元素定位)

    selenium的帮助文档: https://selenium-python.readthedocs.io/api.html#module-selenium.common.exceptions 目前支 ...