搜尋

首頁  >  問答  >  主體

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 天前580

全部回覆(1)我來回復

  • 高洛峰

    高洛峰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 sWi network segmentingle#開一個區域網路

    • 回覆
      0
  • 取消回覆