Browse - Programming Tips - Java: Get remote IP-address of a Socket

Date: 2019dec16
Language: Java

Q.  Java: Get remote IP-address of a Socket

A.
String getRemoteIpAddress(final Socket socket) { return (((InetSocketAddress) socket.getRemoteSocketAddress()).getAddress()).toString().replace("/",""); }
This works for IPv4 and IPv6 socket.getRemoteSocketAddress() includes a slash and the port eg /192.168.1.100:1234