gethostname gethostbyname gethostbyaddr 获得主机相关信息
网络编程里经常需要获得主机的相关信息,下面围绕相关的函数以及用到的结构来说明。
获得主机名:
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 获得主机相关信息的更多相关文章
- 使用gethostname()函数和gethostbyname()函数获取主机相关信息
gethostname() : 返回本地主机的标准主机名. 原型如下: #include <unistd.h> int gethostname(char *name, size_t len ...
- 总结描述用户和组管理类命令的使用方法,系统用户相关信息,取出主机IP地址
1.列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可. [root@db146 ~]# who|cut -f1 -d' ' |sort -u root 2.取出最后 ...
- C#获取当前主机硬件信息
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- php http头设置相关信息
HTTP 状态码 状态码用来告诉HTTP客户端,HTTP服务器是否产生了预期的Response. HTTP/1.1中定义了5类状态码, 状态码由三位数字组成,第一个数字定义了响应的类别 1XX 提示信 ...
- linux lsmod命令 及相关信息
lsmod (list modules) 语法:lsmod 功能: lsmod命令:是一个小程序,用来显示文件.proc/modules的信息,也就是显示当前内核模块装载的模块. 补充说明: 执行l ...
- 使用C语言获取linux系统相关信息
最近在写shell的时候,涉及到了获取环境变量参数和本地计算机相关信息,包括计算机设备名,用户名的信息,在这里简单总结一下.获取环境变量各项参数,可以直接使用getenv函数.man中关于getenv ...
- windows主机网络信息获取程序设计
掌握windows系统获取网络信息的各种API函数的功能与调用方法,掌握设计程序显示获取到相关网络信息的方法,掌握网络字节数据与主机字节数据之间的转换.掌握这些API函数调用的错误处理方法. 利用本地 ...
- [linux]netstat命令详解-显示linux中各种网络相关信息
1.功能与说明 netstat 用于显示linux中各种网络相关信息.如网络链接 路由表 接口状态链接 多播成员等等. 2.参数含义介绍 -a (all)显示所有选项,默认不显示LISTEN相关-t ...
- last 列出登入系统的用户相关信息
Linux last 命令介绍 功能说明:列出目前与过去登入系统的用户相关信息. 语法: last [-adRx][-f <记录文件>][-n <显示列数>][帐号名称. ...
随机推荐
- elasticsearch+filebeat+kibana提取多行日志
filebeat的配置文件filebeat.yml以下三行去掉注释 multiline.pattern: ^\[ multiline.negate: true //false改为true multil ...
- [Codeplus 4月赛]最短路
题意:理论上是给定一张完全图,有边权,在给一些单向边求最短路. 思路: 我充分体会到了我图论的菜. 理论上建图肯定是不能\(n^2\)的,考虑如何优化呢? 将边权异或值二进制替换,最后一遍最短路就行, ...
- Mac+VS Code+Git+Github
https://blog.csdn.net/qq_37747262/article/details/81750417
- python的起源及基本语句
一.Python的起源 Python是吉多范罗苏姆于1989年的圣诞节期间在阿姆斯特丹进行编写的,于1991年编写完成,Python是一门解释型弱类型的编程语言. Python在多个领域中都有应用,比 ...
- JSF(JavaServer Faces)简介
JavaServer Faces (JSF) 是一种用于构建Java Web 应用程序的标准框架(是Java Community Process 规定的JSR-127标准).它提供了一种以组件为中心的 ...
- Windows的DOS命令
f: d: ...
- springboot整合TinyMCE文件上传回显
今天想尝试TinyMCE富文本,准备着手搭建自己的博客,发现springboot上传文件,如果把文件放在static文件夹不能即时回显,百度了下,说是要刷新文件夹才能解决. 有问题就有解决办法 方法1 ...
- JAVA中日期 yyyy-MM-dd HH:mm:ss和yyyy-MM-dd hh:mm:ss的区别
HH是24小时制,hh是12小时制 区别就是:大写的H是二十四小时制的小时数(0-23),小写的h是十二小时制的小时数(am/pm 1-12) //24小时制 SimpleDateFormat sdf ...
- amazeUI tab禁止左右滑动(触控操作)
参考:http://amazeui.clouddeep.cn/javascript/tabs/ 效果: html: <!DOCTYPE html> <html> <hea ...
- 分离vue文件,方便后期维护
参考: https://www.cnblogs.com/wy120/p/10179901.html https://blog.csdn.net/sinat_36146776/article/detai ...