Archived

This topic is now archived and is closed to further replies.

FutureOfNetworks

NetWork Appl. get IP address

Recommended Posts

In short, you can use getsockname() to get the sockaddr structure. You can then cast the sockaddr to a sockaddr_in and access the sin_addr member to see the IP. The in_addr structure has a union that allows you to access the bytes individually, which you'll have to do to convert the 32 bit value into the more familiar dotted-quad string.

Each byte in the value represents one part of the IP, but I forgot which order. Probably network-byte order, but I also forgot what that means as well. (I don't deal with byte-order on a regular basis)

So 127.0.0.1 becomes either 0x0100007F or 0x7F000001

Oh, the socket has to be bound for this to work (via bind()).

EDIT: Scratch all that, you can actually just use the inet_ntoa() function and give it the sin_addr from the sockaddr_in.

[edited by - cgoat on November 1, 2002 2:30:57 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
or just use gethostbyname( "localhost" ) and then print the contents of the hostent structs in_addr array

Share this post


Link to post
Share on other sites