Java InetAddress.getHostName() taking a very long time to execute

后端 未结 3 576
暗喜
暗喜 2021-01-13 01:28

I have the following little code snippet:

        InetAddress address = InetAddress.getByName(host);
        if(address.isReachable(TIMEOUT_IN_MILLISECONDS))         


        
3条回答
  •  长发绾君心
    2021-01-13 01:39

    toString() seems to be faster:

    given an InetAddress ia or InterfaceAddress ia,

    System.out.println ( ia.toString() ) will show a string containing your ipAddress faster than ia.getHostName()

    You can then use ia.toString().substring to extract it.

    I don't know why.

提交回复
热议问题