I am using a time synchronization method to sync local time with a server time. In some cases both the server and the local machine are the same. In this case, I need to find the loopback IP address of the local machine and check if the server IP address is the same. This is the need so I just need an API to find the loopback IP address.
-
4The loopback address is "always" 127.0.0.1 (http://en.wikipedia.org/wiki/Loopback). See http://stackoverflow.com/questions/212528/linux-c-get-the-ip-address-of-local-computer for how to get the public IP. – larsmoa Sep 13 '11 at 06:25
-
Why on Earth do you care? It makes no sense for host to synchronize time with itself. Somebody has to configure the address of the server, so they should make sure they configure one that does make sense. – Jan Hudec Sep 13 '11 at 06:34
-
1The standard NTP solution is to keep track of the stratum. Servers that receive time signal have stratum 1. Any other has 1 + stratum of the server they synchronized to and stratum 10 is considered not synchronized. If the host happens to have itself in the possible sources list will increase it's stratum when it tries to synchronize with itself, which quickly leads to selecting other server (low stratum is preferred) or stopping the synchronization (stratum reaches 10). You are using NTP and not reinventing the wheel, aren't you? – Jan Hudec Sep 13 '11 at 06:41
3 Answers
The loopback address is guaranteed to be 127.x.x.x (any will work, 0.0.1 is standard) on any IPv4 machine and ::1 on any IPv6 machine. There's no need to look this up either - it's always going to work, and on each machine it's going to refer to itself.

- 7,184
- 1
- 29
- 50
That doesn't work. You're assuming that a single machine is represented by a single IP address. That's just not true. An adapter is, and the "loopback" adapter is a virtual one present on every system. But the loopback adapter is unique and distinct from any physical one, and therefore the loopback IP address (127.0.0.1) is distinct from any physical adapter's IP address.
You can get the complete list of local IPv4 addresses with GetAdaptersInfo
. Check if the server address appears in that list. Or simply use NTP, as Jan Hudec suggested.

- 173,980
- 10
- 155
- 350
The loop back address in any in the range of addresses from 127.0.0.1 to 127.255.255.255. You can chose any one you wish. Most people usually chose 127.0.0.1. No API is required.

- 59,252
- 17
- 87
- 127