[英]C++ - getaddrinfo() - getting own external IP with dynamic dns fails
I'm currently working on a program that tells me, among other things, my external IP. 我目前正在开发一个程序,该程序除其他外可以告诉我我的外部IP。 To achieve this, I'm using the function
getaddrinfo()
. 为此,我使用了
getaddrinfo()
函数。
For testing purpose I passed google.com
as node name, it worked fine. 为了进行测试,我通过
google.com
作为节点名称,它工作正常。
If I'm passing my dynamic dns to the function, I receive 0.0.0.0
as IP, but I'm expecting my own external IP. 如果将动态dns传递给该函数,
0.0.0.0
收到0.0.0.0
作为IP,但是我希望有自己的外部IP。 I also tried a dynamic dns from a friend with the same result. 我还尝试了一个来自朋友的动态dns,结果相同。
As far as I can get the IP from google.com
, the problem shouldn't be my code. 就我可以从
google.com
获取IP而言,问题不应该出在我的代码上。
Is it not possible, to receive the IP from a dynamic dns with getaddrinfo()
or is there anything else I'm doing wrong? 使用
getaddrinfo()
从动态dns接收IP是不可能的,还是我做错了什么?
Edit: 编辑:
WSADATA wsaD {};
addrinfo hints = {AI_NON_AUTHORITATIVE, AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, NULL, NULL, NULL};
addrinfo *infoAddress;
WSAStartup(MAKEWORD(2, 2), &wsaD);
getaddrinfo("daniel-nas.computer", "80", &hints, &infoAddress);
struct sockaddr_in *ipAddress = reinterpret_cast<sockaddr_in*>&infoAddress->ai_addr);
inet_ntop(AF_INET, &ipAddress->sin_addr.s_addr, IP, sizeof(IP));
WSACleanup();
IP
is globaly defined: char IP[INET_ADDRSTRLEN];
IP
是char IP[INET_ADDRSTRLEN];
定义的: char IP[INET_ADDRSTRLEN];
Is there anybody else who came over this issue? 还有其他人解决这个问题吗?
[Solution] [解]
As a kind of solution, I'm using the function gethostbyname()
. 作为一种解决方案,我正在使用函数
gethostbyname()
。 Nevertheless I'm not really pleased with it, because the function is deprecated, but I'm now able to get my IP. 不过,我对此并不满意,因为该功能已被弃用,但是现在我可以获得IP。 Therefore I think, the issue is either my code, although I can resolve other hostnames, or the combination of the function
getaddrinfo()
and DDNS. 因此,我认为,问题可能是我的代码,尽管我可以解析其他主机名,或者是函数
getaddrinfo()
和DDNS的组合。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.