分类: C/C++
2017-03-11 18:46:55
gethostbyname 和 gethostbyaddr 函数声明如下:
#include
struct hostent *gethostbyname(const char *nmae);
//返回:成功则为非NULL指针,若出错则为NULL指针,同时设置h_errno.
struct hostent *gethostbyaddr(const char *addr, int len, 0);
//返回:成功则为非NULL指针,若出错则为NULL指针,同时设置h_errno.
结构体hostent
点击(此处)折叠或打开
示例代码如下:
点击(此处)折叠或打开
编译
执行结果如下:
[root@CTOServer001 testprogram]# gcc -g exam002.c -o exam002
[root@CTOServer001 testprogram]# ./exam002
official hostname:
alias:
address: 119.75.217.109
address: 119.75.218.70
[root@CTOServer001 testprogram]# ./exam002 127.0.0.1
official hostname: localhost
alias: localhost.localdomain
alias: localhost4
alias: localhost4.localdomain4
address: 127.0.0.1
[root@CTOServer001 testprogram]#