BSD socket problem: inet_ntop returns "0.0.0.0"

I am trying to get the IP address of a computer that is listening on the socket that I have connected. The port number that is being printed works fine, but the address is "0.0.0.0". Here is the relevant code. res was passed to getaddrinfo and getsockname before moving on 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?

+4
source share
1 answer

An address of 0.0.0.0 means that the socket is listening on all addresses. A specific address, for example 127.0.0.1 , means that the server is simply listening on this address, but not on others.

+6
source

Source: https://habr.com/ru/post/1286761/


All Articles