//------------------------------------------------------------------- // iplookup.cpp // // This program shows how we can reimplement the functionality // of our earlier Python program ('iplookup.py') using the C++ // programming language, which affords us a better view of the // role the 'sockets' application programming interface plays. // // compile using: $ g++ iplookup.cpp -o iplookup // execute using: $ ./iplookup // // programmer: ALLAN CRUSE // written on: 28 JAN 2009 //------------------------------------------------------------------- #include // for gethostbyname() #include // for strncpy() #include // for printf() #include // for inet_ntop() #define BUFLEN INET_ADDRSTRLEN int main( int argc, char **argv ) { char hostname[ 64 ] = { 0 }; if ( argc == 1 ) strcpy( hostname, "localhost" ); else strncpy( hostname, argv[ 1 ], 63 ); char hostip[ BUFLEN ] = { 0 }; struct hostent *hostinfo = gethostbyname( hostname ); if ( !hostinfo ) strcpy( hostip, "unknown" ); else inet_ntop( AF_INET, hostinfo->h_addr, hostip, BUFLEN ); printf( "The IP-address for \'%s\' is %s \n", hostname, hostip ); }