InetAddress.toString() returns a forward slash

后端 未结 2 720
情话喂你
情话喂你 2021-02-18 19:46

I have a variable packet of type DatagramPacket. While packet.getAddress().toString() results in a String representing an the IP address, it has an ext

2条回答
  •  孤城傲影
    2021-02-18 20:07

    Use the following:

    packet.getAddress().getHostAddress()
    

    From the documentation:

    Returns the IP address string in textual presentation.

    Contrast that with InetAddress.toString():

    Converts this IP address to a String. The string returned is of the form: hostname / literal IP address. If the host name is unresolved, no reverse name service lookup is performed. The hostname part will be represented by an empty string.

提交回复
热议问题