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 ...
随机推荐
- CF1155E Guess the Root
题意:交互,有一个10次多项式,你可以询问50次当x为某个取值时多项式的值.你需要回答这个多项式的零点. 解:询问11个数然后高斯消元解方程得出系数.然后枚举定义域看有无0点. #include &l ...
- antd Tree组件中,自定义右键菜单
最近项目中,有一个需求是自定义antd的Tree组件的右键菜单功能. 直接上代码 class Demo extends Component { state = { rightClickNodeTree ...
- django-crontab实现定时任务
django-crontab实现服务端的定时任务 安装 pip install django-crontab 在Django项目中使用 settings.py INSTALLED_APPS = ( ' ...
- CMakeList.txt(3): 一个cmake实例
介绍一个比较实用的例子,即包含生成静态库又包含引入外部头文件和链接库的cmake demo. 先按照工程规范建立工程目录,并编写代码,以下面的工程目录为例进行解释这个例子,工程的目录结构为: 1. 编 ...
- I2C(一)框架
目录 I2C(一)框架 引入 整体框架 数据结构 文件结构 流程简述 参考文档 title: I2C(一)框架 date: 2019/1/28 17:58:42 toc: true --- I2C(一 ...
- 多模块项目Module must not contain source root. The root already belongs to module
多模块项目Module "*" must not contain source root *. The root already belongs to module "* ...
- python3抓图学习-百度贴吧
# coding=utf-8 from bs4 import BeautifulSoup import urllib.request import os import time def downlao ...
- 关于selenium的8种元素定位
selenium中有八种元素定位,分别是:id,name,class_name,tag_name,link_text.partial_link_text.xpath.css 简单的定位可以用 id.n ...
- 论文阅读笔记(七)YOLO
You Only Look Once: Unified, Real-Time Object Detection Joseph Redmon, CVPR, 2016 1. 之前的目标检测工作将分类器用作 ...
- GX/GZOI2019 day2 解题报告
GX/GZOI2019 day2 解题报告 题目链接 逼死强迫症 旅行者 旧词 t1 逼死强迫症 显然地,记 \(f(i)\) 为长度为 \(i\) 的木板的答案,可得: \(\\\) \[f(i)= ...