c - Get Ip address function not working well -
i'm trying ip address of server, gets 127.0.1.1 instead of 127.0.0.1. error? how can real ip address, not localhost address.
#define maxhostname 256 #define debug char * getipaddress() { char myname[ maxhostname + 1 ]; static char ipinascii[ maxhostname ]; /* oversized */ struct hostent * hp; memset( myname, 0, maxhostname + 1 ); /* init */ memset( ipinascii, 0, maxhostname ); gethostname( myname, maxhostname ); #ifdef debug printf( "hostname %s\n", myname ); #endif /* debug */ hp = gethostbyname( myname ); if( hp == null ) { perror( "gethostbyname" ); return( "ip not found" ); } inet_ntop( hp->h_addrtype, hp->h_addr_list[ 0 ], ipinascii, maxhostname ) ; #ifdef debug printf( "canonical hostname %s @ ip %s\n", hp->h_name, ipinascii ); #endif /* debug */ return( ipinascii ); }
user@user-desktop:~/desktop/my code$ ./ipprint hostname user-desktop canonical hostname user-desktop @ ip 127.0.1.1 127.0.1.1
that's /etc/hosts
indicates. has like:
127.0.0.1 localhost 127.0.1.1 user-desktop
Comments
Post a Comment