obtain hostname or IP
I was just wondering if there was any way that I could obtain the hostname or IP address of the machine that my program will be running on?
I have found examples to get the ip address, but i need to know the hostname.
I have also found examples to get the hostname... but i need the ip. lol
So is there any way of finding out one, without knowing the other?
Hope this makes sense ;-)
A single computer may have more than one IP address and more than one hostname. Every computer replies to the loopback interface, 127.0.0.1. The hostname 'localhost' is usually bound to this.
But take, for example, my router. In the internal network, it responds to 192.168.1.1, known as tuxserver, www.webloc.local, www.hpr.local, ...
In the external network, it responds to 62.something, with the only DNS entry being some userid.myprovider.at or something, although one other used to work (but my IP changed).
So it's far from definite. What you CAN try is to enumerate the network devices (but don't ask me how) and obtain the IP of each. Then you can do a reverse DNS lookup, it should give you more than one hostname.
The information you obtain from these functions may not be useable outside the local network, as described above.
gethostname(): The gethostname function retrieves the standard host name for the local computer.
gethostbyname(): The gethostbyname function retrieves host information (ip address) corresponding to a host name.
getsockname(): The getsockname function retrieves the local name (ip address) for a connected socket.
Your external IP address