1

I have two programs that communicate to each other and one piece of data sent is the IP address stored as an unsigned long integer. It is retrieved in one program via the sockaddr_in struct when it accepts an outside connection. I then stored the IP address from the sin_addr member of the struct and the s_addr sub-member which returns the unsigned long version of the IP address.

I look here http://www.retran.com/beej/inet_ntoaman.html and it suggests that I can get the textual (human readable) version of the IP address by using the inet_ntoa function, but it expects the in_addr struct as a parameter.

Is there a function I can use that returns the textual IP address using an unsigned long integer as the parameter?

Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
  • As a side note, when sending "binary" information from one computer to another, you should convert it from "native" byte order to "network" byte order prior to sending it, and vice-versa upon receipt. The functions to look at are: htons(), htonl(), ntohs(), and ntohl(). – TonyB Oct 18 '15 at 06:46
  • As for your exact question my (unhelpful) answer would "not that I'm aware of", but the `sin_addr` member from your `sockaddr_in` structure *is* a `struct in_addr`, on which you can use `inet_ntoa()`. So I'm not sure what's the problem here? – jbm Oct 18 '15 at 07:36
  • And BTW, looking at my `/usr/include/netinet/in.h`, I see `typedef uint32_t in_addr_t; struct in_addr { in_addr_t s_addr; };`, so this boil down to an integer anyway, and simple method such as this should work: http://stackoverflow.com/a/19216093/5257515 – jbm Oct 18 '15 at 07:49

0 Answers0