Advertisement

Getting my own IP address

Started by May 04, 2004 09:37 AM
2 comments, last by Structural 20 years, 9 months ago
Is there a way for retrieving my own IP address using standard sockets (ie: no Microsoft specific implementations)? I need to know this for responding to clients discovering servers on a LAN. TY
STOP THE PLANET!! I WANT TO GET OFF!!
#include <arpa/inet.h>#include <netdb.h>#include <stdio.h>#include <unistd.h>int main( int argc, char **argv ){        char hn[256], *ip;        struct hostent *he;        gethostname( hn, 256 );        he = gethostbyname( hn );        ip = inet_ntoa( *(struct in_addr *)*he->h_addr_list );        printf( "%s\n", ip );        return 0;}
Advertisement
That looks like it would work... were it not that one of the platforms I''m writing for has no DNS functionality. Thus gethostbyname() is not available nor any other name resolving functions
STOP THE PLANET!! I WANT TO GET OFF!!
getsockname man page.

getsockname from MSDN.

Anyway, the client should get the server address using getpeername() or by calling recvfrom(); that way, NAT will work.

Also, getsockname() is broken for UDP on Windows:

kb article on how broken it is.



[edited by - hplus0603 on May 5, 2004 2:47:57 AM]
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement