Getting IPV4 address from a sockaddr structure

Felipe Salazar picture Felipe Salazar · Aug 14, 2009 · Viewed 106.3k times · Source

How can I extract an IP address into a string? I can't find a reference that tells me how char sa_data[14] is encoded.

Answer

Emil H picture Emil H · Aug 14, 2009

Just cast the entire sockaddr structure to a sockaddr_in. Then you can use:

char *ip = inet_ntoa(their_addr.sin_addr)

To retrieve the standard ip representation.