[PLUG] getting IP address of host

Terry Griffin griffint at pobox.com
Wed Apr 30 16:40:28 UTC 2008


> Hello:
> I often have programs that operate a server and one or more clients on a
> local network. I have always had a data file on my server with the
> address at which my server is to listen (the port is hard coded). When I
> move the program to another computer, I need to change my IP address
> data file. I would like to modify my program to get the IP address of
> it's host not knowing the host name. Any ideas?
> Thanks,
> Wayne
>

If you want something that works most of the time then just grab the IP
address of eth0 from "ifconfig eth0" output. If you want something that
works all of the time, well, that can be surprisingly tricky, say if
there is no eth0 or if it's not configured. And even if eth0 is present
and configured, it may have been intended that eth1 have the primary
IP address for the host?

This topic came up on the kernel list a couple years ago and it turns
out there is no universal answer to knowing which IP address of possibly
several is meant to be primary IP address for a host.

Terry




More information about the PLUG mailing list