一、gethostbyname函数原型

#include <netdb.h>

struct hostent *gethostbyname(const char *ghostname);

    返回:成功返回非空指针,出错为NULL且设置h_errno

二、hostent结构

struct hostent {
char *h_name; /* official name of host */
char **h_aliases; /* pointer to array of pointers to alias name */
int h_addrtype; /* host address type: AF_INET */
int h_length; /* length of address: 4 */
char **h_addr_list; /* ptr to array of ptrs with IPv4 addrs */
};

三、关于全局整体变量h_errno

  当gethostbyname发生错误时,它不设置errno变量,而是将全局变量h_errno设置为<netdb.h>中定义的下列常值之一:

  (1)HOST_NOT_FOUND;

  (2)TRY_AGAIN;

  (3)NO_RECOVERY;

  (4)NO_DATA(等同于NO_ADDRESS)

  NO_DATA错误表示指定的名字有效,但是它没有A记录

四、gethostbyname例子

#include    <stdio.h>
#include <netdb.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h> #define INET_ADDRSTRLEN 16 void err_ret(const char *, ...);
void err_msg(const char *, ...); int main(int argc, char **argv)
{
char *ptr, **pptr;
char str[INET_ADDRSTRLEN];
struct hostent *hptr; while (--argc > ) {
ptr = *++argv;
if ( (hptr = gethostbyname(ptr)) == NULL) {
err_msg("gethostbyname error for host: %s: %s",
ptr, hstrerror(h_errno));
continue;
}
printf("official hostname: %s\n", hptr->h_name); for (pptr = hptr->h_aliases; *pptr != NULL; pptr++) {
printf("\talias: %s\n", *pptr);
} switch (hptr->h_addrtype) {
case AF_INET: {
pptr = hptr->h_addr_list;
for ( ; *pptr != NULL; pptr++) {
printf("\taddress: %s\n",
inet_ntop(hptr->h_addrtype, *pptr, str, sizeof(str)));
}
break;
}
default: {
err_ret("unknown address type");
break;
}
}
}
exit();
}
#include    <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> /* ANSI C header file */
#include <syslog.h> /* for syslog() */ #define MAXLINE 4096 int daemon_proc; /* set nonzero by daemon_init() */ static void err_doit(int, int, const char *, va_list); /* Nonfatal error related to system call
* Print message and return */ void err_ret(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_INFO, fmt, ap);
va_end(ap);
return;
} /* Fatal error related to system call
* Print message and terminate */ void err_sys(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
exit();
} /* Fatal error related to system call
* Print message, dump core, and terminate */ void err_dump(const char *fmt, ...) {
va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(); /* shouldn't get here */
} /* Nonfatal error unrelated to system call
* Print message and return */ void err_msg(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_INFO, fmt, ap);
va_end(ap);
return;
} /* Fatal error unrelated to system call
* Print message and terminate */ void err_quit(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
exit();
} /* Print message and return to caller
* Caller specifies "errnoflag" and "level" */ static void err_doit(int errnoflag, int level, const char *fmt, va_list ap) { int errno_save, n;
char buf[MAXLINE + ]; errno_save = errno; /* value caller might want printed */
#ifdef HAVE_VSNPRINTF
vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
#else
vsprintf(buf, fmt, ap); /* not safe */
#endif
n = strlen(buf);
if (errnoflag)
snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
strcat(buf, "\n"); if (daemon_proc) {
syslog(level, buf);
} else {
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr);
}
return;
}

五、gethostbyaddr函数原型

#include <netdb.h>

struct hostent *gethostbyaddr(const struct in_addr *, socklen_t, int family);

    返回:成功为非空指针,否则为NULL且设置h_errno
功能:由一个二进制IP地址找到相应的主机名

gethostbyname和gethostbyaddr的更多相关文章

  1. UNIX网络编程——名字与地址转换(gethostbyname,gethostbyaddr,getservbyname,getservbyport,getaddrinfo,getnameinfo函数)

    名字和数值地址间进行转换的函数:gethostbyname和gethostbyaddr在主机名字与IPv4地址之间进行转换.getservbyname和getservbyport在服务器名字和端口号之 ...

  2. linux网络编程——域名转换 gethostbyname与gethostbyaddr

    域名转换 #include <netdb.h> struct hostent *gethostbyname(const char *name); 参数: name: 执行主机名的指针 返回 ...

  3. 关于 getsockname、getpeername和gethostname、gethostbyname

    一.gethostname,gethostbyname的用法 这两个函数可以用来获取主机的信息.gethostname:获取主机的名字gethostbyname:通过名字获取其他的信息(比如ip) 1 ...

  4. 一个进程发起多个连接和gethostbyname等函数

    一.在前面讲过的最简单的回射客户/服务器程序中,一个客户端即一个进程,只会发起一个连接,只要稍微修改一下就可以让一个客户端发起多个连 接,然后只利用其中一个连接发送数据. 先来认识一个函数getsoc ...

  5. [TCPIP] DNS Note

    TCPIP DNS  域名系统 DNS 是一个应用于TCP/IP应用程序的分布式数据库,它提供主机名字和IP地址之间的转换及有关电子邮件的选路信息. 对DNS的访问是通过一个地址解析器来完成的,在Un ...

  6. getaddrinfo

    gethostbyname和gethostbyaddr这两个函数仅仅支持IPv4,getaddrinfo函数能够处理名字到地址以及服务到端口这两 种转换,返回的是一个sockaddr结构的链表而不是一 ...

  7. socket编程相关的结构体和字节序转换、IP、PORT转换函数

    注意:结构体之间不能直接进行强制转换, 必须先转换成指针类型才可以进行结构体间的类型转换, 这里需要明确的定义就是什么才叫强制转换. 强制转换是将内存中一段代码以另一种不同类型的方式进行解读, 因此转 ...

  8. linux下socket编程

    相关结构 //下边这两个结构定义在<sys/types.h>里 //一般的地址结构,只能用于覆盖(把其他地址转换为此类型),且只能引用该地址的sa_family字段 struct sock ...

  9. squid 学习笔记

    Squid学习笔记 1.安装前的配置 编译安装之前需要校正的参数主要包括File Descriptor和Mbuf Clusters. 1.File Descriptor 查看文件描述符的限制数目: u ...

随机推荐

  1. luogu4145 上帝造题的七分钟2 (线段树)

    题意:给一个数列,维护两个操作,区间开根号.询问区间和 注意到1e12开根号六次后就变成1,而且根号1等于1 也就是说,就算我们用单点修改,只要跳过1,那么修改的次数最多也就是6n 那么维护一个区间最 ...

  2. POJ-3687 Labeling Balls(拓扑)

    不一样的拓扑排序 给定一些标记为1到n的数, 求出满足a < b 的序列, 如果有多个输出, 按先标签1往前的位置, 然后按标签2往前的位置, 对于每个标签, 位置都尽量往前. 因为位置要往前, ...

  3. socket,tcp,http三者之间的区别和原理

    http.TCP/IP协议与socket之间的区别下面的图表试图显示不同的TCP/IP和其他的协议在最初OSI模型中的位置: 7 应用层 例如HTTP.SMTP.SNMP.FTP.Telnet.SIP ...

  4. js 函数声明和函数表达式的区别

    javascript中声明函数的方法有两种:函数声明和函数表达式. 区别如下: 1).以函数声明的方法定义的函数,函数名是必须的,而函数表达式的函数名是可选的. 2).以函数声明的方法定义的函数,函数 ...

  5. A1131. Subway Map (30)

    In the big cities, the subway systems always look so complex to the visitors. To give you some sense ...

  6. 第三十二节,使用谷歌Object Detection API进行目标检测、训练新的模型(使用VOC 2012数据集)

    前面已经介绍了几种经典的目标检测算法,光学习理论不实践的效果并不大,这里我们使用谷歌的开源框架来实现目标检测.至于为什么不去自己实现呢?主要是因为自己实现比较麻烦,而且调参比较麻烦,我们直接利用别人的 ...

  7. Day23--Python--常用模块02--序列化,configparser

    1. 序列化 把对象打散成bytes或者字符串. 方便存储和传输 序列化 把bytes或者字符串转换回对象. 反序列化 2. pickle(比较重要) 把python中所有的对象都可以转化成bytes ...

  8. rm刷机 root

    http://www.miui.com/download-290.html http://www.miui.com/shuaji-329.html    小米稳定版 不可以root 只有升级到上面的开 ...

  9. Percona xtrabackup

    关于percona xtrabackup     percona xtrabackup是世界上唯一开源的.免费的MySQL热备份软件,可以为InnoDB存储引擎和XtraDB存储引擎进行不间断的备份, ...

  10. CentOS7利用systemctl添加自定义系统服务

    CentOS7的每一个服务以.service结尾,一般会分为3部分:[Unit].[Service]和[Install] 转载于互联网 [Unit] 部分主要是对这个服务的说明,内容包括Descrip ...