getHostAddress() and getInetAddress() in Java

前端 未结 2 1334
孤独总比滥情好
孤独总比滥情好 2020-12-21 00:16

I am creating TCP socket application. In server side,

ss = new ServerSocket(10000);
Socket socket = ss.accept();
String remoteIp = socket.getInetAddress().g         


        
相关标签:
2条回答
  • 2020-12-21 00:56

    socket.getInetAddress() returns an InetAddress object that contains the IP address of the remote machine.

    InetAddress.getHostAddress() returns a String object with the textual representation of that address.

    So, to end up with a String you can print, that's how you do it.

    Edit: In case you're not familiar, this is called 'method chaining'. It's the same thing as saying:

    InetAddress addy = socket.getInetAddress();
    String remoteIp = addy.getHostAddress();
    
    0 讨论(0)
  • 2020-12-21 01:09

    In addition to Brian Roachs answer:

    You can also take a look into the Java API to find a description for classes, methods and fields:

    • Socket.getInetAddress()
    • InetAddress.getHostAddress()
    0 讨论(0)
提交回复
热议问题