search

Home  >  Q&A  >  body text

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

PHPzPHPz2889 days ago574

reply all(1)I'll reply

  • 高洛峰

    高洛峰2017-04-17 17:52:38

    • 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

    reply
    0
  • Cancelreply