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?