Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to find the IP address of current machine in C

Reply
Thread Tools

How to find the IP address of current machine in C

 
 
venutaurus539@gmail.com
Guest
Posts: n/a
 
      05-29-2009
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.
 
Reply With Quote
 
 
 
 
Jim
Guest
Posts: n/a
 
      05-29-2009
On 2009-05-29, http://www.velocityreviews.com/forums/(E-Mail Removed) <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
venutaurus539@gmail.com
Guest
Posts: n/a
 
      05-29-2009
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 <(E-Mail Removed)> wrote:
> On 2009-05-29, (E-Mail Removed) <(E-Mail Removed)> 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.


 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      05-29-2009
On 29 May 2009 at 9:48, (E-Mail Removed) 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;
}

 
Reply With Quote
 
dfighter
Guest
Posts: n/a
 
      05-29-2009
(E-Mail Removed) 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
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      05-29-2009
Richard wrote:

> dfighter <(E-Mail Removed)> writes:
>
>> (E-Mail Removed) 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

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      05-29-2009
In article <gvopbf$sg6$(E-Mail Removed)>,
Chris Dollin <(E-Mail Removed)> 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...

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      05-29-2009
Antoninus Twink wrote:
> On 29 May 2009 at 9:48, (E-Mail Removed) 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
 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      05-29-2009
Han from China <(E-Mail Removed)> 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
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      05-29-2009
In article <(E-Mail Removed)>,
Gordon Burditt <(E-Mail Removed)> 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"); }

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Win32 install error "for a machine type other than current machine =?Utf-8?B?Sm9obiBEeWVy?= Windows 64bit 4 05-10-2007 06:04 PM
Find out the MAC address of a machine. UJ ASP .Net 4 09-02-2005 10:46 PM
Machine type other than the current machine? =?Utf-8?B?REpheS1a?= Windows 64bit 7 08-18-2005 04:00 PM



Advertisments