winsock2域名换取ip地址,gethostbyname的使用(避坑)

版权声明:博主最近都在,有不懂疑惑的,可以一起交流,也希望能有高人指点一二,谢啦!本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40295575/article/details/86569795

在做那个用域名去换取ip地址的功能时候,在查阅相关资料上遇到很多打算,看了n多篇博客都无法解决,最后查看到了,发现按照大多数博客自己跑起来都不行,最后找到了一个解决方法

#include <WinSock2.h>
#include <iostream>
#pragma comment(lib, "ws2_32.lib")
int main()
{
	 WORD wVersionRequested;
     WSADATA wsaData;
     int err;
     wVersionRequested = MAKEWORD(1, 1);
     err = WSAStartup(wVersionRequested, &wsaData);
     if (err != 0) {
         return 0;
     }
	hostent *host = gethostbyname("yun.wyuyun.xin");
	//printf("%s",host->h_addr_list[0]);
    printf("%s\n",inet_ntoa(*((in_addr *)host->h_addr_list[0])));
}

在list那,可能有多条可以写for循环遍历。

解决思路来源
https://www.cnblogs.com/danmoqingshan/p/9316175.html

猜你喜欢

转载自blog.csdn.net/weixin_40295575/article/details/86569795