linux网络编程——域名转换 gethostbyname与gethostbyaddr
域名转换
#include <netdb.h>
struct hostent *gethostbyname(const char *name);
参数: name: 执行主机名的指针
返回值: 返回一个hostent指针
struct hostent
{
char *h_name; // 表示主机的规范名
char **h_aliases; // 表示主机的别名,别名可能有多个
int h_addrtype; // 表示的是主机ip地址的类型, ipv4 AF_iNET 或 ipv6 AFINET6
int h_length; // 表示主机ip地址的长度
char ** h_addr_list; // 表示的是主机的ip地址
}
#include <stdio.h>
#include <netdb.h>
#include <arpa/inet.h>
int main(void)
{
struct hostent* hent;
int i = 0;
char addr[16];
// 通过域名获取IP信息
hent = gethostbyname("www.baidu.com");
printf("h_name: %s\n", hent->h_name);
while (hent->h_aliases[i] != NULL)
printf("aliase: %s\n", hent->h_aliases[i++]);
i = 0;
while (hent->h_addr_list[i] != NULL)
printf("ip addr %s\n", inet_ntop(hent->h_addrtype,hent->h_addr_list[i++], addr, sizeof(addr)));
return 0;
}

还有一个类似的函数 gethostbyaddr,通过ip地址获取到规范名,别名等信息


#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
int main(int argc, char **argv)
{
struct in_addr addr;
struct hostent *phost;
if (inet_pton(AF_INET, argv[1], &addr) <= 0) {
printf("inet_pton error:%s\n", strerror(errno));
return -1;
}
phost = gethostbyaddr((const char*)&addr, sizeof(addr), AF_INET);
if (phost == NULL) {
printf("gethostbyaddr error:%s\n", strerror(h_errno));
return -1;
}
printf("host name:%s\n", phost->h_name);
return 0;
}


linux网络编程——域名转换 gethostbyname与gethostbyaddr的更多相关文章
- Linux网络编程入门 (转载)
(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...
- [转] - Linux网络编程 -- 网络知识介绍
(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...
- 【转】Linux网络编程入门
(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...
- 《转》Linux网络编程入门
原地址:http://www.cnblogs.com/duzouzhe/archive/2009/06/19/1506699.html (一)Linux网络编程--网络知识介绍 Linux网络编程-- ...
- 很全的linux网络编程技巧
本文转载自:http://www.cnblogs.com/jfyl1573/p/6476607.html 1. LINUX网络编程基础知识 1 1.1. TCP/IP协议概述 1 1.2. OSI参考 ...
- Linux网络编程入门
(一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端 网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...
- Proxy源代码分析——谈谈如何学习Linux网络编程
Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到, Linux和Windows这样的"傻瓜"操作系统(这里丝毫没有贬低Windows的意思,相反这应该 ...
- 第5章 Linux网络编程基础
第5章 Linux网络编程基础 5.1 socket地址与API 一.理解字节序 主机字节序一般为小端字节序.网络字节序一般为大端字节序.当格式化的数据在两台使用了不同字节序的主机之间直接传递时,接收 ...
- Linux网络编程之套接字基础
1.套接字的基本结构 struct sockaddr 这个结构用来存储套接字地址. 数据定义: struct sockaddr { unsigned short sa_family; /* addre ...
随机推荐
- ios21--xib例子
故事板控制器: // // XMGViewController.m // 03-综合练习 // // Created by xiaomage on 15/12/28. // Copyright © 2 ...
- 如何调试Node.js
Debugging Node.js with Chrome DevTools https://nodejs.org/en/docs/guides/debugging-getting-started/ ...
- Resharper 实现接口的方式
- luogu 2627 修建草坪
题目大意: 一个数列,取出一些数使得它们的总和最大且没有k个连续 思路: 首先我们可以找到一个nk的dp dp方程:dp[i]=dp[i-1]+sum[i]-sum[j] (sum[j]尽量小) 然后 ...
- LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP
题目:https://loj.ac/problem/6089 对于 i <= √n ,设 f[i][j] 表示前 i 种,体积为 j 的方案数,那么 f[i][j] = ∑(1 <= k ...
- cURL模拟HTTP请求(支持HTTPS)
function setHttpRequest($url,$headers,$params=array(),$method="GET") { $ci = curl_init(); ...
- J - Ananagrams(map+vector)
Description Most crossword puzzle fans are used to anagrams--groups of words with the same letters i ...
- python2行代码调用程序
import win32api win32api.ShellExecute(0, 'open', r'C:\Users\TOPFEEL\AppData\Local\Postman\app-5.5.0\ ...
- 287 Find the Duplicate Number 寻找重复数
一个长度为 n + 1 的整形数组,其中的数字都在 1 到 n 之间,包括 1 和 n ,可知至少有一个重复的数字存在.假设只有一个数字重复,找出这个重复的数字.注意: 不能更改数组内容(假设数 ...
- Java虚拟机内存详解
概述 Java虚拟机会自动管理内存,不容易出现内存泄漏和内存溢出问题.Java虚拟机会在执行过程中将管理的内存分为若干个不同的数据区域. 运行时数据区域 在jdk1.8之前的版本与1.8版本略有不同, ...