InetAddress.getLocalHost().getHostAddress()的返回值问题?

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地址。
这是为什么呢?

阅读 7.3k
1 个回答
新手上路,请多包涵
  • Addresses in the range 169.254.xxx.xxx are link local IP addresses. These are reserved for use on a single network segment.
    开启了WiFi共享功能后会新开一个局域网

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题