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

 
 
Richard Bos
Guest
Posts: n/a
 
      05-30-2009
Richard <rgrdev_@gmail.com> wrote:

> (Kenny McCormack) writes:
>
> > 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 was fishing.


Nice try, but no dice.

Richard
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      05-30-2009
This is the code I use for getting the IP address associated to a
certain network interface:

char* get_ipv4_addr(char *ifc) {
int sd=socket(AF_INET,SOCK_STREAM,0);
struct ifreq ifr;

strncpy (ifr.ifr_name,ifc,sizeof(ifr.ifr_name));
struct sockaddr_in *sin = (struct sockaddr_in*) &ifr.ifr_addr;

ioctl(sd,SIOCGIFNAME,&ifr);
ioctl(sd,SIOCGIFADDR,&ifr);

if (!sin->sin_addr.s_addr) return NULL;
else return inet_ntoa(sin->sin_addr);
}

p.s. I see more and more gratuitous and very poorly documented trolls
here. Just think about shutting down this newsgroup instead of
re-routing everybody makes a C-related question to other newsgroups, if
that is not a true ANSI C oriented question. This newsgroup gets very
useless in this case.

<> 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.


--
-----BEGIN GEEK CODE BLOCK-----
GCS/CM/CC/E/IT/LS/M d-(--) C++++$ UBL++++$ P++++ L+++++$ E--- W+++ w--
PS+++ PE-- Y++ PGP+++ R++ tv-- b++>+++ D+ G>+++ e++>+++++ h* r++ z+++
------END GEEK CODE BLOCK------
 
Reply With Quote
 
 
 
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-30-2009
Gordon Burditt wrote:
> Most machines that are actually connected to a network have more
> than one IPv4 address, and one of them is "127.0.0.1".


Nope, this is not required at all.

Bye, Jojo
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      05-30-2009
On 30 May 2009 at 18:13, Joachim Schmitz wrote:
> Gordon Burditt wrote:
>> Most machines that are actually connected to a network have more
>> than one IPv4 address, and one of them is "127.0.0.1".

>
> Nope, this is not required at all.


What part of the word "most" is causing you trouble?

 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-30-2009
Antoninus Twink wrote:
> On 30 May 2009 at 18:13, Joachim Schmitz wrote:
>> Gordon Burditt wrote:
>>> Most machines that are actually connected to a network have more
>>> than one IPv4 address, and one of them is "127.0.0.1".

>>
>> Nope, this is not required at all.

>
> What part of the word "most" is causing you trouble?


What part of "and one of them is" is causing you trouble?
 
Reply With Quote
 
Chris McDonald
Guest
Posts: n/a
 
      05-31-2009
Han from China <autistic-> writes:

>Note that comp.unix.programmer posters are happy to deal with
>ISO C as well. So let's shut down comp.lang.c and redirect
>all posters to comp.unix.programmer, where they can get the
>most knowledgeable experts, instead of the phony know-nothings
>like Kiki Thompson and Dicky Heathfield.



Do you feel that, some time in the future, you'll ever be able to enter
a c.l.c. thread without your obligatory attack on individuals?
It will be a pleasure.

--
Chris.
 
Reply With Quote
 
Joachim Schmitz
Guest
Posts: n/a
 
      05-31-2009
Mark McIntyre wrote:
> Joachim Schmitz wrote:
>> Antoninus Twink wrote:
>>> On 30 May 2009 at 18:13, Joachim Schmitz wrote:
>>>> Gordon Burditt wrote:
>>>>> Most machines that are actually connected to a network have more
>>>>> than one IPv4 address, and one of them is "127.0.0.1".
>>>>
>>>> Nope, this is not required at all.
>>>
>>> What part of the word "most" is causing you trouble?

>>
>> What part of "and one of them is" is causing you trouble?

>
> To be fair to the Troll, your comment didn't need the "nope" and would
> have been uncontrovertial without it.


Well, I intereted Gordon's statement as '127.0.0.1 is required', which it is
not.

Bye, Jojo

 
Reply With Quote
 
Rafael
Guest
Posts: n/a
 
      06-03-2009
Gordon Burditt escreveu:

>> I don't even want to extract the
>> IP address from ifconfig because it is time consuming.

>
> That may be the most portable way of doing it, for some reasonable
> guess at what you are looking for.


So... to the OP: Read the ifconfig source and find your code there.

Regards
Rafael
 
Reply With Quote
 
tenga tenga is offline
Junior Member
Join Date: Oct 2011
Posts: 1
 
      10-19-2011
Quote:
Originally Posted by Kenny McCormack View Post
In article <P62dnaMX6IfK1b3XnZ2dnUVZ_g->,
Gordon Burditt <> 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"); }
Kenny you are f...en idiot
 
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