How to get ip address from C application

May 21, 2010 4 Replies

You either need to run a dhcp client on your device(like dhclient or udhcpcd) or implement your own DHCP client in your software.

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

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 gmail.com share ONE personality -- and have since BIRTH!!

Hi Miloserdin.

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

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.

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 ============================================= MicroLAB Systems address: 83 Dubninskaya street, #612, 127591 Moscow, Russia phone/fax: +7-(499)-900-6208 e-mail: oleg.miloserdin@mlabsys.com WEB: http://www.mlabsys.com =============================================

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required