开发者

Is there any API in C++ to find the loopback IP address of a machine?

开发者 https://www.devze.com 2023-04-04 23:23 出处:网络
I am using a time synchronization method to sync local time with a serv开发者_开发技巧er time. In some cases both the server and the local machine are the same. In this case, I need to find the loopba

I am using a time synchronization method to sync local time with a serv开发者_开发技巧er 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号