views:

227

answers:

1

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?

+6  A: 

An address of 0.0.0.0 means that the socket is listening on all addresses. A specific address like 127.0.0.1 would mean that the server is just listening on that address, but not on any other ones.

sth