How to get ip address from C application

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

Reply to
Atilla
Loading thread data ...

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!!
Reply to
Grant Edwards

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

Reply to
steve_schefter

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.

Reply to
David Brown

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
=============================================
Reply to
Miloserdin Oleg

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.