linux下gethostbyname通过域名获取IP的例子
2013-02-27 15:32
211 查看
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int i;
struct hostent *he;
struct in_addr **addr_list;
if (argc != 2) {
fprintf(stderr,"usage: ghbn hostname\n");
return 1;
}
if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
herror("gethostbyname");
return 2;
}
// print information about this host:
printf("Official name is: %s\n", he->h_name);
printf(" IP addresses: ");
addr_list = (struct in_addr **)he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++) {
printf("%s ", inet_ntoa(*addr_list[i]));
}
printf("\n");
return 0;
}
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int i;
struct hostent *he;
struct in_addr **addr_list;
if (argc != 2) {
fprintf(stderr,"usage: ghbn hostname\n");
return 1;
}
if ((he = gethostbyname(argv[1])) == NULL) { // get the host info
herror("gethostbyname");
return 2;
}
// print information about this host:
printf("Official name is: %s\n", he->h_name);
printf(" IP addresses: ");
addr_list = (struct in_addr **)he->h_addr_list;
for(i = 0; addr_list[i] != NULL; i++) {
printf("%s ", inet_ntoa(*addr_list[i]));
}
printf("\n");
return 0;
}
相关文章推荐
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip 、获取本地主机的名字
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
- 调用Linux的busybox,通过linux命令来获取AndRoidIP
- Linux LAN 通过IP获取目标主机的MAC地址
- LINUX下QT与C语言通过网卡名获取网卡IP与MAC
- linux 通过Ip获取主机名等信息gethostbyaddr()等。
- 如何通过httpclient获取访问域名的真实ip
- linux 如何禁止通过IP访问网站,只让用域名访问网站
- C#通过域名获取IP(转)
- python通过域名获取IP简单的GUI界面
- linux c 网络编程:用域名获取IP地址或者用IP获取域名 网络地址转换成整型 主机字符顺序与网络字节顺序的转换
- android常用工具类 通过域名获取ip
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
- java通过ip获取客户端Mac地址的小例子
- linux通过域名获取主机信息
- 通过域名获取IP[转帖]
- linux 环境下 用gethostbyname函数获取 DNS的服务器列表 名称 IP
- Linux C 网络编程 - 获取本地 ip 地址,mac,通过域名获取对应的 ip
- C# .net通过域名获取IP(转)