From: Rainer Weikusat on 23 May 2010 13:58 gazelle(a)shell.xmission.com (Kenny McCormack) writes: > David Schwartz <davids(a)webmaster.com> wrote: >>On May 21, 10:32�am, "Miloserdin Oleg" <oleg.miloser...(a)mlabsys.com> >>wrote: >> >>> How can I get IP address from my application without using /etc/hosts >>> editing? >> >>It's always 127.0.0.1. If that's not the answer you want, you're not >>asking the right question. >> >>DS > > Isn't it actually, 127.0.0.anything? > > (It might be 127.0.anything.anything; I can't remember at the moment...) The 'loopback network' is usually 127/8.
From: ~Glynne on 23 May 2010 22:38 On May 21, 11:32 am, "Miloserdin Oleg" <oleg.miloser...(a)mlabsys.com> wrote: > I'm interesting how to get IP address from C application. I've used > gethostname() and gethostbyname() in my application. I've edited /etc/hosts > file to add my hostname and IP address. My PC is connected to server > which using DHCP. If I remove added lines from /etc/hosts gethostbyname() > function returns error. > How can I get IP address from my application without using /etc/hosts > editing? > > -- > Best regards, > Miloserdin Oleg > > ============================================= > MicroLAB Systems > address: 83 Dubninskaya street, #612, 127591 Moscow, Russia > phone/fax: +7-(499)-900-6208 > e-mail: oleg.miloser...(a)mlabsys.com WEB:http://www.mlabsys.com > ============================================= A quick and dirty idea: use ifconfig and parse out the desired address. Maybe something like: FILE *p = popen( "ifconfig | grep inet.addr: | cut -d: -f2", "r" ); if (p) { char line[512]; while( fgets(line, sizeof(line)-1, p) ) { /* use address for something */ } }
From: Gordon Burditt on 24 May 2010 15:11 >Isn't it actually, 127.0.0.anything? > >(It might be 127.0.anything.anything; I can't remember at the moment...) On FreeBSD, it defaults to 127.*.*.* (network 127.0.0.0/8). However, if you try to ping 127.0.0.1, it works. If you try anything else in that address range, you get "Can't assign requested address". It is possible to do wierd stuff like having Apache use IP-based virtualhosting on parts of that network.
From: Rick Jones on 24 May 2010 17:12 ~Glynne <glynnec2002(a)yahoo.com> wrote: > A quick and dirty idea: use ifconfig and parse out the desired > address. > Maybe something like: > FILE *p = popen( "ifconfig | grep inet.addr: | cut -d: -f2", "r" ); > if (p) { > char line[512]; > while( fgets(line, sizeof(line)-1, p) ) { /* use address for > something */ } > } That will have portability issues: $ uname -a HP-UX tardy B.11.11 U 9000/785 2002119164 unlimited-user license $ ifconfig usage: ifconfig interface [ af ] [ address [ dest_addr ] ] [ up ] [ down ] [ netmask mask ] [ prefix n ] [ metric n ] [ arp | -arp ] [ plumb | unplumb ] and if you are thinking of ifconfig -a that still has those issues: $ ifconfig -a ifconfig: no such interface rick jones 99 times out of 10, 127.0.0.1 will be a usable "loopback" this host IP address, but for that 100th time out of ten do the RFCs actually mandate a loopback interface with 127.mumble be present and active on a system? -- The glass is neither half-empty nor half-full. The glass has a leak. The real question is "Can it be patched?" these opinions are mine, all mine; HP might not want them anyway... :) feel free to post, OR email to rick.jones2 in hp.com but NOT BOTH...
First
|
Prev
|
Pages: 1 2 Prev: where is good sample code I can learn about linux systemprogramming Next: multiplexing |