getHostAddress() and getInetAddress() in Java

前端 未结 2 1362
孤独总比滥情好
孤独总比滥情好 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();
    

提交回复
热议问题