import java.net.Inet4Address; import java.net.InetAddress; import java.net.UnknownHostException; public class Demo { public static void main(String[] args) { InetAddress address = null; try { address = InetAddress.getLocalHost(); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } /** * 169.254.86.58 */ System.out.println(address.getHostAddress()); } }
上面的代码输出169.254.86.58 ,而我机器的IP地址是59开头的。
我的笔记本使用网线连上的网络, 然后开启了WiFi共享的功能,然后运行上面的代码结果输出的是169.254.86.58,如果我关闭了WiFi共享功能, 那么上面的代码就能够输出59开头的IP地址。
这是为什么呢?
Addresses in the range 169.254.xxx.xxx are link local IP addresses. These are reserved for use on a single network segment.
After turning on the WiFi sharing function, a new LAN will be opened