Linux下C实现通过域名得到IP

一直想学Linux网络编程
并且固执的要用c来写  认为这样才是最纯正牛x的
结果c写网络太难了.....
我想实现一个http的简单的get操作
居然没有找到一个例子
气死我了
Linux下的学习资料太少  那 我就来补充吧

gethostbyname()
真是相当简单 我终于可以自己写一个网络程序了
/***************************************************************************
 *   Copyright (C) 2008 by root   *
域名  转  IP地址
 ***************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>

int main(int argc, char *argv[])
{
 struct hostent *h;
 char hostname[40];
 printf("请输入域名/n");
 scanf("%s",hostname);
 getchar();
 if((h=gethostbyname(hostname))==NULL)
 {
     fprintf(stderr,"不能得到IP/n");
     exit(1);
 }
 printf("HostName :%s/n",h->h_name);
 printf("IP Address :%s/n",inet_ntoa(*((struct in_addr *)h->h_addr)));
  return EXIT_SUCCESS;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux C 语言中,可以使用 getaddrinfo 函数将域名转换为 IP 地址。 具体实现步骤如下: 1. 引入头文件: ``` #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h> ``` 2. 定义 getaddrinfo() 函数所需的结构体: ``` struct addrinfo hints, *res; int status; char ipstr[INET6_ADDRSTRLEN]; ``` 3. 初始化 hints 结构体: ``` memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; // AF_INET 或 AF_INET6 均可 hints.ai_socktype = SOCK_STREAM; // TCP ``` 4. 调用 getaddrinfo() 函数: ``` status = getaddrinfo("www.example.com", NULL, &hints, &res); ``` 其中 "www.example.com" 是需要转换的域名。 5. 遍历返回的地址列表,并将其转换为 IP 地址: ``` struct addrinfo *p; void *addr; for(p = res; p != NULL; p = p->ai_next) { if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr); } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; addr = &(ipv6->sin6_addr); } // 将 IP 地址转换为字符串 inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); } ``` 其中,inet_ntop() 函数将 IP 地址转换为字符串形式,ipstr 变量即为转换后的 IP 地址。 6. 释放内存: ``` freeaddrinfo(res); ``` 完整代码如下: ``` #include <sys/types.h> #include <sys/socket.h> #include <netdb.h> #include <arpa/inet.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { struct addrinfo hints, *res; int status; char ipstr[INET6_ADDRSTRLEN]; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; status = getaddrinfo("www.example.com", NULL, &hints, &res); if (status != 0) { fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } struct addrinfo *p; void *addr; for(p = res; p != NULL; p = p->ai_next) { if (p->ai_family == AF_INET) { // IPv4 struct sockaddr_in *ipv4 = (struct sockaddr_in *)p->ai_addr; addr = &(ipv4->sin_addr); } else { // IPv6 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)p->ai_addr; addr = &(ipv6->sin6_addr); } inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr); printf("IP address: %s\n", ipstr); } freeaddrinfo(res); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值