IP ADDRESS OF MACHINE

Andrea 'Fyre Wyzard' Bocci fwyzard@inwind.it
Wed Feb 27 19:32:00 GMT 2002


I have this one working on my Linux machine:

/* ip.c */
#include <unistd.h>
#include <netdb.h>

#define SIZE 256

char hostname[SIZE];
char **alias;
char **entry;
struct hostent *host;

int main (int argc, char **argv) {
   gethostname (hostname, SIZE);
   host = gethostbyname (hostname);

   printf ("host name:  %s\n", host->h_name);
   for (alias = host->h_aliases; *alias; alias++)
     printf ("alias:      %s\n", *alias);
   for (entry = host->h_addr_list; *entry; entry++)
     printf ("address:    %hhu.%hhu.%hhu.%hhu\n", \
       (*entry)[0], (*entry)[1] , (*entry)[2] , (*entry)[3]);

   return 0;
}
/* end of ip.c */

The output is

host name:  stufis06.fisica.unifi.it
alias:      stufis06
address:    150.217.141.25

HTH, fwyzard



More information about the Gcc-help mailing list