tags:

views:

655

answers:

2

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.

+3  A: 

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.

Emil H
+1. Depending on platform, remember to check the family first. There may not be an IPV4 address to extract...
Steve Jessop
+1  A: 

This article explains the contents of the sockaddr struct:

http://h30097.www3.hp.com/docs/base%5Fdoc/DOCUMENTATION/V50A%5FHTML/MAN/MAN7/0052%5F%5F%5F%5F.HTM

Once cast to sockaddr_in, it becomes this:

 struct sockaddr_in {
      u_short sin_family;
      u_short sin_port;
      struct in_addr sin_addr;
      char   sin_zero[8];
       };
Amber