Hi,
How can I get local IP adress within a C program?
I tried this with gethostname function but it can not be helped since i dnt know the host name of the machine.
Thanks
Hi,
How can I get local IP adress within a C program?
I tried this with gethostname function but it can not be helped since i dnt know the host name of the machine.
Thanks
i tried with gethostname.
but it returns the hostname like "myLaptop".
I just need the ip like 192.168.10.10
my prev code was like as follows
Code:memset( hostname , 0 , MAXHOSTNAMELEN ); gethostname( hostname,MAXHOSTNAMELEN ); printf("MAXHOSTNAMELEN = %i \n " , MAXHOSTNAMELEN);
Passing your hostname to gethostbyname() should work. Depending on how your system is set up though, this may just return "127.0.0.1".
could you please give a some code example for this?
now my programe is as follows.
here then how can i get the ip and print it onto the console?Code:char *hostname = malloc(MAXHOSTNAMELEN ); memset( hostname , 0 , MAXHOSTNAMELEN ); gethostname( hostname,MAXHOSTNAMELEN ); struct hostent hostn = mallock(sizeof(struct hostent)); gethostbyname(hostname);
There is a lot of samples in the net...
Winsock Programmer's FAQ: Get the Local IP Address(es) - for example
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Hi,
Finaly i found the fellowing programs to obtain the local ip .
but it returns 127.0.1.1
please advice what goes wrong with this?
Thanks.Code:struct hostent *he; struct in_addr a; int main (int argc) { char *hostname = malloc(MAXHOSTNAMELEN ); memset( hostname , 0 , MAXHOSTNAMELEN ); gethostname( hostname,MAXHOSTNAMELEN ); printf("hostname = %s \n " , hostname) ; he = gethostbyname (hostname); if (he) { printf("name: %s\n", he->h_name); while (*he->h_aliases) printf("alias: %s\n", *he->h_aliases++); while (*he->h_addr_list) { bcopy(*he->h_addr_list++, (char *) &a, sizeof(a)); printf("address: %s\n", inet_ntoa(a)); } } else printf("error \n"); //herror(argv[0]); return 0; }
I think this has no way to work out since my machine name (which I get by gethostname() method) is not in a DNS.
so I think it returns 127.0.0.1
any ideas about it?
Thanks.
sory its 127.0.1.1
Are you on windows or linux?
On linux, gethostbyname() will just return whatever is in /etc/hosts. You can always add a line to the hosts file which has your correct IP address.
Another option is to run "ipconfig" or "ifconfig" (depending on your OS), and parse out the result.
What you want is the address of the actual interface. You can't really get at this information in a portable way.
gethostbyname() is portable, but if it returns multiple answers you have no way of knowing which one is correct.
Honestly, I'd bite the bullet and use a non-portable method to query the network interface and obtain its address directly.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Going with bithub's suggestion will be your best bet, especially if you have many interfaces, say a dual homed server with tied up vlans to them.