BSD 套接字问题:inet_ntop 返回“0.0.0.0”
我正在尝试获取我绑定的套接字正在侦听的机器的 IP。 打印的端口号工作正常,但地址是“0.0.0.0”。 这是相关代码。 在获取此代码之前,res
已传递给 getaddrinfo
和 getsockname
。
char ip[INET_ADDRSTRLEN];
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
void* addr = &(ipv4->sin_addr);
inet_ntop(res->ai_family, addr, ip, sizeof ip);
std::cout << "SERVER_ADDRESS " << ip << std::endl;
std::cout << "SERVER_PORT " << ipv4->sin_port << std::endl;
可能出什么问题了?
I'm trying to get the IP of the machine a socket I've bound is listening on. The port number printed works fine, but the address is "0.0.0.0". Here's the relevant code. res
has been passed to getaddrinfo
and getsockname
before getting to this code.
char ip[INET_ADDRSTRLEN];
struct sockaddr_in *ipv4 = (struct sockaddr_in *)res->ai_addr;
void* addr = &(ipv4->sin_addr);
inet_ntop(res->ai_family, addr, ip, sizeof ip);
std::cout << "SERVER_ADDRESS " << ip << std::endl;
std::cout << "SERVER_PORT " << ipv4->sin_port << std::endl;
What could be wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
地址
0.0.0.0
表示套接字正在侦听所有地址。 像127.0.0.1
这样的特定地址意味着服务器仅侦听该地址,而不侦听任何其他地址。An address of
0.0.0.0
means that the socket is listening on all addresses. A specific address like127.0.0.1
would mean that the server is just listening on that address, but not on any other ones.