![]() |
How to find the IP address of current machine in C
Hi All,
Can some one please let me know how to find the ip address of a machine within a C program. I've tried gethostbyname() but it uses / etc/hosts file which is not I want. I don't even want to extract the IP address from ifconfig because it is time consuming. So, can any one let me know how can I do this without effecting portability across linux flavors (No Windows please). Awaiting your reply, Thank you, Venu Madhav. |
Re: How to find the IP address of current machine in C
On 2009-05-29, venutaurus539@gmail.com <venutaurus539@gmail.com> wrote:
> Hi All, > Can some one please let me know how to find the ip address of > a machine within a C program. I've tried gethostbyname() but it uses / > etc/hosts file which is not I want. I don't even want to extract the > IP address from ifconfig because it is time consuming. So, can any one > let me know how can I do this without effecting portability across > linux flavors (No Windows please). Keep in mind that a machine can have several IPs, including (but not limited to) one for each network card. Jim -- http://www.ursaMinorBeta.co.uk http://twitter.com/GreyAreaUK Please help save Bletchley Park - sign the petition for Government funding at: (open to UK residents and ex.pats) http://petitions.number10.gov.uk/BletchleyPark/ Thank you. |
Re: How to find the IP address of current machine in C
Thanks for the reply,
Yeah, I know that. Is there any way to get that list of IPs of a machine. Thank you, Venu Madhav On May 29, 3:19*pm, Jim <j...@magrathea.plus.com> wrote: > On 2009-05-29, venutaurus...@gmail.com <venutaurus...@gmail.com> wrote: > > > Hi All, > > * * * * *Can some one please let me know how to find the ip address of > > a machine within a C program. I've tried gethostbyname() but it uses / > > etc/hosts file which is not I want. I don't even want to extract the > > IP address from ifconfig because it is time consuming. So, can any one > > let me know how can I do this without effecting portability across > > linux flavors (No Windows please). > > Keep in mind that a machine can have several IPs, including (but not limited > to) one for each network card. > > Jim > --http://www.ursaMinorBeta.co.uk*http://twitter.com/GreyAreaUK > Please help save Bletchley Park - sign the petition for > Government funding at: * * (open to UK residents and ex.pats)http://petitions.number10.gov.uk/BletchleyPark/* *Thank you. |
Re: How to find the IP address of current machine in C
On 29 May 2009 at 9:48, venutaurus539@gmail.com wrote:
> Can some one please let me know how to find the ip address of a > machine within a C program. I've tried gethostbyname() but it uses / > etc/hosts file which is not I want. The program below will list all your up network interfaces and their IP addresses. Note that if you're behind a router/firewall/etc. then the IP address will probably be internal to your LAN, as with ifconfig. To get the "external-facing" IP address, I don't think there's any better way than using HTTP to download a page from a site like whatismyip.org and then scraping the IP it reports. #include <stdio.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <net/if.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> #include <sys/ioctl.h> int main(void) { int fd; struct if_nameindex *curif, *ifs; struct ifreq req; if((fd = socket(PF_INET, SOCK_DGRAM, 0)) != -1) { ifs = if_nameindex(); if(ifs) { for(curif = ifs; curif && curif->if_name; curif++) { strncpy(req.ifr_name, curif->if_name, IFNAMSIZ); req.ifr_name[IFNAMSIZ] = 0; if (ioctl(fd, SIOCGIFADDR, &req) < 0) perror("ioctl"); else printf("%s: [%s]\n", curif->if_name, inet_ntoa(((struct sockaddr_in*) &req.ifr_addr)->sin_addr)); } if_freenameindex(ifs); if(close(fd)!=0) perror("close"); } else perror("if_nameindex"); } else perror("socket"); return 0; } |
Re: How to find the IP address of current machine in C
venutaurus539@gmail.com wrote:
> Hi All, > Can some one please let me know how to find the ip address of > a machine within a C program. I've tried gethostbyname() but it uses / > etc/hosts file which is not I want. I don't even want to extract the > IP address from ifconfig because it is time consuming. So, can any one > let me know how can I do this without effecting portability across > linux flavors (No Windows please). > > Awaiting your reply, > > Thank you, > Venu Madhav. "The C programming language" cannot handle networking by default. I suggest you post in comp.unix.programmer as they are the most competent regarding unix programming issues (that includes network programming) -- dfighter |
Re: How to find the IP address of current machine in C
Richard wrote:
> dfighter <dfighter@gm.halp.plis> writes: > >> venutaurus539@gmail.com wrote: >>> Hi All, >>> Can some one please let me know how to find the ip address of >>> a machine within a C program. I've tried gethostbyname() but it uses / >>> etc/hosts file which is not I want. I don't even want to extract the >>> IP address from ifconfig because it is time consuming. So, can any one >>> let me know how can I do this without effecting portability across >>> linux flavors (No Windows please). >>> >>> Awaiting your reply, >>> >>> Thank you, >>> Venu Madhav. >> "The C programming language" cannot handle networking by default. >> I suggest you post in comp.unix.programmer as they are the most >> competent regarding unix programming issues (that includes network >> programming) > > What on earth makes you think it's Unix? Linux maybe? "portability across linux flavors" and "/etc/hosts" are what we in the comprehension business call "clues". -- "I see a great hand reaching out of the stars." Elric, /Babylon 5/ Hewlett-Packard Limited registered office: Cain Road, Bracknell, registered no: 690597 England Berks RG12 1HN |
Re: How to find the IP address of current machine in C
In article <gvopbf$sg6$1@news-pa1.hpl.hp.com>,
Chris Dollin <chris.dollin@hp.com> wrote: .... (rgrdev asked) >> What on earth makes you think it's Unix? Linux maybe? > >"portability across linux flavors" and "/etc/hosts" are what we in the >comprehension business call "clues". One of the common bits of the Usenet religion (*) is that Linux is not Unix. Note that this statement may be technically true (for certain sets of definitions and beliefs), but that's not the point. The point is that for all practical purposes, Linux is "a Unix" and to maintain otherwise is, well, to use the term from my previous post, "pushing an agenda". I must admit that I was surprised to see that sort of post coming from rgrdev, who is normally a quite rational poster (aka, "troll", in the jargon of CLC). (*) I'm using the word "religion" here as a general umbrella for man's irrational beliefs. Everybody has some... |
Re: How to find the IP address of current machine in C
Antoninus Twink wrote:
> On 29 May 2009 at 9:48, venutaurus539@gmail.com wrote: >> Can some one please let me know how to find the ip address of a >> machine within a C program. I've tried gethostbyname() but it uses / >> etc/hosts file which is not I want. > > The program below will list all your up network interfaces and their IP > addresses. Note that if you're behind a router/firewall/etc. then the IP <snip> On one of my Linux servers it does not. The OP should go to either comp.unix.programmer or one of the Linux groups if s/he wants to know how to get all of the addresses. -- Flash Gordon |
Re: How to find the IP address of current machine in C
Han from China <autistic-pedantry@comp.lang.c> wrote:
> like Kiki Thompson and Dicky Heathfield. Hattie, you are getting more childish by the minute. I expect that from Kenny, not from you. (IOW: please keep better track of your personae.) Richard |
Re: How to find the IP address of current machine in C
In article <P62dnaMX6IfK1b3XnZ2dnUVZ_g-dnZ2d@posted.internetamerica>,
Gordon Burditt <gordonb.5ezfy@burditt.org> wrote: >> Can some one please let me know how to find the ip address of >>a machine within a C program. > >Standard C doesn't provide networking facilities. > >Is the ip address you want that of the machine you're running on? >If not, the way to find it is via DNS lookup (e.g. gethostbyname() >). > >Most machines that are actually connected to a network have more >than one IPv4 address, and one of them is "127.0.0.1". Most machines You know, I just realized that the best answer to this question is... (Are you ready for it? Drum roll, please...) 127.0.0.1 Simple, effective, portable (see implementation below, which should meet the standards of this NG), and likely to be very efficient. #include <stdio.h> int main(void) { puts("(at least one of) Your IP address(es) is: 127.0.0.1"); } |
| All times are GMT. The time now is 04:33 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.