linux下编程实现将域名地址转换为IP地址
2013-03-07 12:27
465 查看
struct hostent结构体
这个数据结构是这样的:struct hostent {
const char *h_name; // official name of host
char **h_aliases; // alias list
short h_addrtype; // host address type
short h_length; // length of address
char **h_addr_list; // list of addresses from name server
#define h_addr h_addr_list[0] // address, for backward compatiblity
};
typedef uint32_t in_addr_t;
struct in_addr
{
in_addr_t s_addr;
};
这里是这个数据结构的详细资料:
struct hostent:
h_name – 地址的正式名称。
h_aliases – 空字节-地址的预备名称的指针。
h_addrtype –地址类型; 通常是AF_INET。
h_length – 地址的比特长度。
h_addr_list – 零字节-主机网络地址指针。网络字节顺序。
h_addr - h_addr_list中的第一地址。
gethostbyname() 成 功时返回一个指向结构体 hostent 的指针,或者 是个空 (NULL) 指针。
这里是个例子:
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
int main(void) {
struct hostent *h;
h = gethostbyname("www.126.com");
if(h==NULL){
herror("gethostbyname");
exit(1);
}
printf("%s\n",h->h_name);
printf("%d\n",h->h_addr);
struct in_addr *in={h->h_addr};
printf("%s\n",inet_ntoa(*in));
// printf("IP Address : %s\n",inet_ntoa(*((struct in_addr *)h->h_addr)));
return EXIT_SUCCESS;
}
在使用 gethostbyname() 的时候,你不能用perror() 打印错误信息 (因为 errno 没有使用),你应该调用 herror()。
gethostbyname()返回的 struct hostent 数据。
相关文章推荐
- linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
- linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
- linux下nginx实现虚拟主机(3种方法:基于域名、基于端口、基于ip地址)
- 如何将域名地址转换成对应的IP地址?
- 如何编程实现修改IP地址、子网掩码、网关、DNS地址等?
- Linux 网络编程——网络字节序、地址转换
- 如何编程实现修改IP地址、子网掩码、网关、DNS地址等?
- linux应用程序编程之网络(用域名取得主机的ip地址)
- Linux 网络编程——网络字节序、地址转换
- (转载)Linux网络编程-使用gethostbyaddr,通过ip地址,得到该ip地址的主机的信息
- Linux下用C实现域名到IP的转换(域名解析)
- Linux 网络编程——网络字节序、地址转换
- Linux网络编程03——字节序和地址转换
- Linux下由域名编程解析ip地址 (段错误(吐核))
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
- Linux下C编程实现---获取本机IP地址
- IP地址与域名之间的转换(Linux + GCC)
- 【linux网络编程】网络字节序、地址转换
- linux下底层函数实现MAC、IP地址以及网关地址