Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServerSocket + client Socket - how do I get IP address of client?

I have a ServerSocket instance which is listening for connections. When a client connects to it, I would like to get the IP of the connected socket, but can't seem to find the right method to do so.

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

I've tried calling the following with no luck:

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

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

What am I doing wrong?

like image 498
Alex Blundell Avatar asked Mar 14 '13 22:03

Alex Blundell


1 Answers

connectionSocket.getRemoteSocketAddress();
like image 199
Shmil The Cat Avatar answered Sep 19 '22 05:09

Shmil The Cat