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

获得主机名:
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. windows下 Mysql 8.0.x 数据库简单的导出和导入!!!

    1.首先需要进入到mysql安装目录下的bin目录,执行cmd进入命令窗口. 2.导出(导出某个数据库,也可以针对某张表导出)2.1导出数据结构以及数据的命令: mysqldump -u root - ...

  2. 【JZOJ6367】工厂(factory)

    description 大神 wyp 开了家工厂,工厂有 n 个工人和 p 条流水线. 工厂的工人都是睡神,因此第 i 个工人只会在 si 至 ti 时刻才会工作. 每个工人都会被分派到一条流水线上, ...

  3. sql语句之分组

    对聚合函数的结果进行筛选用having,不能用where

  4. VS2008编译出现问题:error C2485: “__restrict”: 无法识别的扩展属性 解决办法

    错误:Error3 error C2485: '__restrict' : unrecognized extended attribute f:\program files\microsoft vis ...

  5. getResourceAsStream的几种路径配置(properties文件的路径问题)

    文章目录 getResourceAsStream有以下几种: getResourceAsStream有以下几种: Class.getResourceAsStream(String path) : pa ...

  6. LightOJ-1007-Mathematically Hard-欧拉函数打表+前缀和+预处理

    Mathematically some problems look hard. But with the help of the computer, some problems can be easi ...

  7. 一个因为系统字号设置导致的rem计算渲染异常问题

    测试同学突然拿着一部手机过来说,H5渲染各个元素都变大了,有些元素撑出了屏幕外面. 本来以为是某个Webview的渲染兼容问题,结果发现所有的浏览器都这样. 莫名其妙,隐约感觉是 rem计算出了问题, ...

  8. java-day09

    接口 就是一种公共规范标准,只要符合规范标准,就可以大家通用,多个类的公告规范,引用数据类型 格式 public interface 接口名称{} 接口都能定义抽象方法 public abstract ...

  9. wpf 几种常用控件样式

    转自:http://blog.csdn.net/xuejiren/article/details/39449515

  10. linux centos&Ubuntu&RedHat更换软件源

    linux三大版本更换软件源 前排提示,有的源可能已经失效,如果发现请留言或自行寻找其他源. 1.centos mv /etc/yum.repos.d/CentOS-Base.repo [--path ...