gethostbyname
SYNOPSIS
#include <netdb.h> struct hostent *gethostbyname(const char *name);
Data Structure
http://www.cnblogs.com/LubinLew/p/POSIX-DataStructure.html#struct_hostent
Description
gethostbyname 是不可重入函数,在多线程编程时需要注意, linux中有可重入版本 gethostbyname_r,
POSIX标准里面使用getaddrinfo和getnameinfo来替换gethostbyname系列函数了,这些函数已经已经被废弃了。
Parameter
参数 name 既可以是一个主机名(例如"www.163.com"),也可以是IPv4地址(点分十进制形式,例如"111.202.60.48")或者IPv6地址(冒号分隔16进制地址.例如"2000::1:2345:6789:abcd").
如果参数是IPv4或者IPv6地址,那么函数不会执行查询操作,返回的结构体中 h_name 就是该参数的拷贝, h_addr_list[0]就是该参数对应的的整型网络字节(相当于inet_pton(AF_INET*, name, dst)).
If name doesn’t end in a dot and the environment variable HOSTALIASES is set, the alias file pointed to by HOSTALIASES will first be searched for name (see hostname(7) for the file format).
The current domain and its parents are searched unless name ends in a dot.
Example
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h> int main(void)
{
int i = ;
char str[] = {};
struct hostent* phost = NULL; phost = gethostbyname("www.163.com");
if (NULL == phost)
{
return -;
} printf("---Offical name:\n\t%s\n", phost->h_name); printf("---Alias name:\n");
for (i = ; phost->h_aliases[i]; i++)
{
printf("\t%s\n", phost->h_aliases[i]);
} printf("---Address list:\n");
for (i = ; phost->h_addr_list[i]; i++)
{
printf("\t%s\n", inet_ntop(phost->h_addrtype, phost->h_addr_list[i], str, sizeof(str)-1));
} return ;
}
运行结果
---Offical name:
163.xdwscache.glb0.lxdns.com
---Alias name:
www.163.com
www.163.com.lxdns.com
---Address list:
111.202.60.48
111.202.60.47
gethostbyname的更多相关文章
- C++:通过gethostbyname函数,根据服务器的域名,获取服务器IP
本代码的编译环境为MAC,系统版本为10.11.6: #include <string.h> #include <netdb.h> #include <stdio.h&g ...
- php中的gethostbyname函数有问题
在根据域名获取ip的批量执行中,gethostbyname有些域名得到的ip是不正确的,不知道是不是版本的bug. 解决办法是,使用执行命令的方式获取 echo exec("host dom ...
- windows下gethostbyname 调用失败
gethostbyname()函数属于WinSock API库,而在使用WinSock API之前,必须调用WSAStartup函数,只有该函数成功返回(表示应用程序与WinSock库成功地建立起连接 ...
- gethostbyname 亲测可用
建立Socket链接的时候需要IP地址,但是只有域名怎么办,gethostbyname就是一个将域名转换为IP的函数: #include <netdb.h> struct hostent ...
- python的socket里 gethostbyname 与 gethostbyname_ex 的区别
python里有一个模块,叫socket,提供了BSD socket 的通信接口,在看了这个模块之后,我发现了两个很相似的函数------gethostbyname 和gethostbyname_ex ...
- Linux 网络编程: gethostbyname( ), getservbyname( )
前言 最近在学习网络编程,用到几个应该比较常用的网络编程函数,所以写篇博客来记录一下,毕竟学得快忘得也快.国庆节在宿舍写着博客看着各个景点人山人海倒也快哉~ gethostbyname( ) 这个函数 ...
- gethostbyname() -- 用域名或主机名获取IP地址
#include <netdb.h> #include <sys/socket.h> struct hostent *gethostbyname(const char * ...
- gethostbyname()函数说明
gethostbyname()函数说明——用域名或主机名获取IP地址 包含头文件 #include <netdb.h> #include <sys/socket.h> ...
- vc根据域名获取IP地址 gethostbyname()函数
以下是VC Socket初始化时用到的两个函数 一.WSAStartup函数 int WSAStartup ( ...
- 关于 getsockname、getpeername和gethostname、gethostbyname
一.gethostname,gethostbyname的用法 这两个函数可以用来获取主机的信息.gethostname:获取主机的名字gethostbyname:通过名字获取其他的信息(比如ip) 1 ...
随机推荐
- Borůvka algorithm
Borůvka algorithm 我好无聊啊,直接把wiki的算法介绍翻译一下把. wiki关于Borůvka algorithm的链接:链接 Borůvka algorithm是一个在所有边权都是 ...
- redis基础知识思维导图
看到一张redis的基础知识思维导图,比较清晰, 但是没有标明来源,希望知道的给个地址,我也好说明来源 图大小有1M多.在博客上看比较模糊,可以先下载下来查看,或者在浏览器新标签中打开图片查看,就比较 ...
- JSP三大指令 六大内置对象
(1)include指令 作用: 在当前页面用于包含其他页面 语法: <%@include file=”common/header.jsp”%> (2)page指令 作用: 告诉tomca ...
- codeforces-1142 (div1)
div1真难,现在就是后悔, 非常后悔 A.显然如果我们知道起点和终点是哪两个点,我们可以算出距离通过b / gcd(a,b)的方式求出需要走几步的. 并且数据范围似乎也允许我们这么做,所以直接枚举取 ...
- freetype之PC机体验
目录 freetype之PC机体验 引入 中文教程 官方教程 代码结构 字体概念 PC上安装 官方例子 宽字符保存显示中文 坐标框架体系 字符坐标信息获取 title: freetype之PC机体验 ...
- 深入浅出mybatis之缓存机制
目录 前言 准备工作 MyBatis默认缓存设置 缓存实现原理分析 参数localCacheScope控制的缓存策略 参数cacheEnabled控制的缓存策略 总结 前言 提到缓存,我们都会不约而同 ...
- uCos-II中任务的同步与通信
任务的同步与通信 任务间的同步 在多任务合作工作过程中,操作系统要解决两个问题: 各任务间应该具有一种互斥关系,即对某些共享资源,如果一个任务正在使用,则其他任务只能等待,等到该任务释放资源后,等待任 ...
- 五十三、linux 编程——TCP 编程基本介绍
53.1 socket 套接字 53.1.1 介绍 Socket(套接字)是一种通讯机制,它包含一整套的调用接口和数据结构的定义,它给应用进程提供了使用如 TCP/UDP 灯网络协议进行网络通讯的手段 ...
- 【OpenGL】代码记录01创建窗口
创建空窗口: #include<iostream> // GLEW #define GLEW_STATIC #include <GL/glew.h> // GLFW #incl ...
- C#多线程处理
创建多线程,并带参数! using System; using System.Collections; using System.Collections.Generic; using System.I ...