ServerSocket + client socket - how to get client IP address?

I have an instance of ServerSocket that listens for connections. When a client connects to it, I would like to get the IP address of the connected socket, but I can not find a suitable method for this.

public void start() {
    listenSocket = new ServerSocket(port);
    connectionSocket = listenSocket.accept();
}

I tried calling the following with no luck:

connectionSocket.getLocalAddress();
connectionSocket.getInetAddress.getHostAddress();
listenSocket.getLocalSocketAddress();

None of the above will return the correct IP address. They either return "/ 0: 0: 0: 0: 0: 0: 0: 1% 0" or "0.0.0.0".

What am I doing wrong?

+5
source share
1 answer
connectionSocket.getRemoteSocketAddress();
+13
source

All Articles