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?
Ben s source share