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

获得主机名:
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. day33 序列类型,绑定方法,类方法,静态方法,封装继承和多态

    Python之路,Day20 = 序列类型,绑定方法,类方法,静态方法,封装继承和多态 序列是指有序的队列,重点在"有序". 一.Python中序列的分类 Python中的序列主要 ...

  2. tomcat7 linux service

    1. 创建tomcat用户 useradd -r -m -d /usr/local/tomcat7 -s /sbin/nologin tomcat 2. 将下面脚本命名为tomcat7 放入/etc/ ...

  3. System.Web.UI.WebControls.FileUpload.cs

    ylbtech-System.Web.UI.WebControls.FileUpload.cs 1. 程序集 System.Web, Version=4.0.0.0, Culture=neutral, ...

  4. Java笔记 – JDBC编程

    JDBC通过分层技术实现了跨数据库编程.为不同的数据库开发了统一的编程接口,为不同的数据库提供了不同的JAR类库. 一.JDBC基础 1.开发环境 (1)下载对应的Jar包 Oracle的本地Jar包 ...

  5. service sshd start启动失败,Badly formatted port number.

    在做xhell学习的时候,把端口号修改了,后面忘记修改回 来,导致 [root@MyRoth 桌面]# service sshd start 正在启动 sshd:/etc/ssh/sshd_confi ...

  6. vue之.native修饰符

    .native 修饰符就是用来注册元素的原生事件而不是组件自定义事件的 比如:自定义 Button.vue 组件 <template> <button type="butt ...

  7. neo4j算法(1)-介绍

    neo4j为图数据库,其中涉及的也就为图算法,图算法被用来度量图形,节点及关系. 在neo4j中,通过call algo.list() 可查看neo4j中的算法列表. 在neo4j官方文档中,主要记录 ...

  8. 2019-11-9-win10-支持默认把触摸提升-Pointer-消息

    title author date CreateTime categories win10 支持默认把触摸提升 Pointer 消息 lindexi 2019-11-09 15:32:31 +0800 ...

  9. python pip安装扩展报错

    1.安装tldr报错 (1)报错详情: [root@linuxnode1 ~]# pip install tldrCollecting tldr Downloading https://files.p ...

  10. 【左偏树】 [JLOI2015]城池攻占

    原来左偏树还可以打tag,get了 和线段树打tag一样,时不时Push_Down就好了 然后这里显然也是要先乘法后加法的 tag打上了之后还是其他一般左偏树差不多,有些细节注意一下 然后开 long ...