How to get ip address from C application

Do you have a question? Post it now! No Registration Necessary

Translate This Thread From English to

Threaded View
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 board's hostname and IP address. My board 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

We've slightly trimmed the long signature. Click to see the full one.
Re: How to get ip address from C application

Quoted text here. Click to load it
You either need to run a dhcp client on your device(like dhclient or udhcpcd) or
  implement your own DHCP client in your software.

Re: How to get ip address from C application

Quoted text here. Click to load it

You're going to have to explain what you mean by "get IP address".

Quoted text here. Click to load it

Again, what do you mean by "get IP address"?

The following function will "get IP address":

char *foo(void)
{
   return "129.168.32.98";
}

Unless you you can tell us what IP address you want to get, that's as
good a solution as any.

--
Grant Edwards               grant.b.edwards        Yow! The entire CHINESE
                                  at               WOMEN'S VOLLEYBALL TEAM all
We've slightly trimmed the long signature. Click to see the full one.
Re: How to get ip address from C application
Hi Miloserdin.

Quoted text here. Click to load it

There are a few options to get your address.  You can use
popen("ifconfig eth0"...) and parse the output.  Or you can use an
SIOCGIFADDR ioctl on a socket (google SIOCGIFADDR for the details).

     Steve

Re: How to get ip address from C application
Quoted text here. Click to load it

You can try to set up a socket and use getsockname() - I've done that
with Python, so I guess the C code can use the same principle.

Site Timeline