Thread: get nic ip of localhost

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    351

    get nic ip of localhost

    Hi there,

    before you ask, the search engine only accepts words with over two/three chars.

    i've been looking for a way of finding the ip bound to the nic interface of the localhost (NOT 127.0.0.1).

    i do not necessarily know the name of the machine either.

    any suggestions or links?

    TIA, rotis23

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    ok, this code gets the nics ip (in linux eth0) using the system hostname.

    On linux it is important to have the network setup correctly or you will get localhost (/etc/hosts for example).

    this will not work if dhcp is being used.

    not all error checking has been included
    Code:
    struct hostent *hostinfo;
    	char *host,**addrs;
    	char myname[256];
    
    	gethostname(myname, 255);
    	host = myname;
    
    	hostinfo = gethostbyname(host);
    	if(!hostinfo)
    	{
    		fprintf(stderr, "cannot get info for host: %s\n", host);
    		exit(1);
    	}
    	
    	addrs = hostinfo -> h_addr_list;
    
    	while(*addrs)
    	{
    		printf(" %s", inet_ntoa(*(struct in_addr *)*addrs));
    		addrs++;
    	}
    	
    	printf("\n");

  4. #4
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: get nic ip of localhost

    Originally posted by rotis23
    Hi there,

    before you ask, the search engine only accepts words with over two/three chars.

    i've been looking for a way of finding the ip bound to the nic interface of the localhost (NOT 127.0.0.1).

    i do not necessarily know the name of the machine either.

    any suggestions or links?

    TIA, rotis23
    the ip bound to "localhost" will NEVER EVER EVER EVER be anything BUT 127.0.0.1
    hello, internet!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 07:17 PM
  2. Need help to obtain NIC IP and MAC in C
    By kingsz1 in forum Networking/Device Communication
    Replies: 4
    Last Post: 10-13-2006, 11:29 AM
  3. Blocking a server with hosts.
    By adrianxw in forum Tech Board
    Replies: 4
    Last Post: 03-14-2003, 12:14 AM