使用gethostname()函数和gethostbyname()函数获取主机相关信息
2016-05-24 19:56
701 查看
from http://ty1992.blog.51cto.com/7098269/1685880
gethostname() : 返回本地主机的标准主机名。
原型如下:
#include <unistd.h>
int gethostname(char *name, size_t len);
参数说明:
这个函数需要两个参数:
接收缓冲区name,其长度必须为len字节或是更长,存获得的主机名。
接收缓冲区name的最大长度
返回值:
如果函数成功,则返回0。如果发生错误则返回-1。错误号存放在外部变量errno中。
gethostbyname()函数说明——用域名或主机名获取IP地址
包含头文件
#include <netdb.h>
#include <sys/socket.h>
函数原型
struct hostent *gethostbyname(const char *name);
这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。
返回hostent结构体类型指针
hostent->h_name
表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com。
hostent->h_aliases
表示的是主机的别名.www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
hostent->h_addrtype
表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是pv6(AF_INET6)
hostent->h_length
表示的是主机ip地址的长度
hostent->h_addr_lisst
表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :
这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。返回指向dst的一个指针。如果函数调用错误,返回值是NULL。
实例如下:
编译运行
-----------------------------
# gcc -o getinfo getinfo.c
# ./getinfo
hostname: www.server1.com
ip: 69.172.201.208
注意:试验时主机名要是域名格式(如www.server1.com,若函数为server1时gethostbuname函数返回为NULL),gethostbyname()函数才能获取到信息,否则返回NULL
本文出自 “划舞鱼” 博客,请务必保留此出处http://ty1992.blog.51cto.com/7098269/1685880
gethostname() : 返回本地主机的标准主机名。
原型如下:
#include <unistd.h>
int gethostname(char *name, size_t len);
参数说明:
这个函数需要两个参数:
接收缓冲区name,其长度必须为len字节或是更长,存获得的主机名。
接收缓冲区name的最大长度
返回值:
如果函数成功,则返回0。如果发生错误则返回-1。错误号存放在外部变量errno中。
gethostbyname()函数说明——用域名或主机名获取IP地址
包含头文件
#include <netdb.h>
#include <sys/socket.h>
函数原型
struct hostent *gethostbyname(const char *name);
这个函数的传入值是域名或者主机名,例如"www.google.cn"等等。传出值,是一个hostent的结构。如果函数调用失败,将返回NULL。
返回hostent结构体类型指针
1 2 3 4 5 6 7 8 | struct hostent { char *h_name; /* official name of host */ char **h_aliases; /* alias list */ int h_addrtype; /* host address type */ int h_length; /* length of address */ char **h_addr_list; /* list of addresses */ } #define h_addr h_addr_list[0] /* for backward compatibility */ |
表示的是主机的规范名。例如www.google.com的规范名其实是www.l.google.com。
hostent->h_aliases
表示的是主机的别名.www.google.com就是google他自己的别名。有的时候,有的主机可能有好几个别名,这些,其实都是为了易于用户记忆而为自己的网站多取的名字。
hostent->h_addrtype
表示的是主机ip地址的类型,到底是ipv4(AF_INET),还是pv6(AF_INET6)
hostent->h_length
表示的是主机ip地址的长度
hostent->h_addr_lisst
表示的是主机的ip地址,注意,这个是以网络字节序存储的。千万不要直接用printf带%s参数来打这个东西,会有问题的哇。所以到真正需要打印出这个IP的话,需要调用inet_ntop()。
const char *inet_ntop(int af, const void *src, char *dst, socklen_t cnt) :
这个函数,是将类型为af的网络地址结构src,转换成主机序的字符串形式,存放在长度为cnt的字符串中。返回指向dst的一个指针。如果函数调用错误,返回值是NULL。
实例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h> #include <sys/socket.h> #include <netdb.h> #include <unistd.h> #include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h> void handler( int sig){ printf ( "recv a sig=%d\n" ,sig); exit (EXIT_SUCCESS); } #define ERR_EXIT(m) \ do { \ perror (m); \ exit (EXIT_FAILURE);\ } while (0); int main( void ) { char host[100] = {0}; if (gethostname(host, sizeof (host)) < 0){ ERR_EXIT( "gethostname" ); } struct hostent *hp; if ((hp=gethostbyname(host)) == NULL){ ERR_EXIT( "gethostbyname" ); } int i = 0; while (hp->h_addr_list[i] != NULL) { printf ( "hostname: %s\n" ,hp->h_name); printf ( "ip: %s\n" ,inet_ntoa(*( struct in_addr*)hp->h_addr_list[i])); i++; } return 0; } |
-----------------------------
# gcc -o getinfo getinfo.c
# ./getinfo
hostname: www.server1.com
ip: 69.172.201.208
注意:试验时主机名要是域名格式(如www.server1.com,若函数为server1时gethostbuname函数返回为NULL),gethostbyname()函数才能获取到信息,否则返回NULL
本文出自 “划舞鱼” 博客,请务必保留此出处http://ty1992.blog.51cto.com/7098269/1685880
相关文章推荐
- 工具类系列-GenerateHtml
- string类的运算符重载
- adb常见命令。查看内存,内存占用率,抓取log等
- 第十三周--动物这样叫
- mysql_基础1
- UE中使用正则表达式
- ACM--打表--HDOJ 1999--不可摸数
- 打印1到最大的n位数
- Android 解决中文参数传递到服务器乱码问题
- QuickHull 快速凸包
- LoadRunner中winsocket协议学习
- iOS:Objective-C中Self和Super详解
- 一年犹太人读书64本中国人4本反映啥
- Unicode 转化 GB18030 编码 方法
- 我所知道的关于webview的知识
- C/c++语言sort函数如何使用
- ZOJ1089(dfs)
- Best Time to Buy and Sell Stock
- 图片缓存之内存缓存技术LruCache,软引用
- 方法的类型提示