Reputation: 1866
I have a socket listener java based running on the desktop end and I have a mobile client, which is able to connect with this socket server without any issues. I want to have the socket connection established for each client separately, when client is trying to access new connection. I read from this link, Reference link for multiple connection detection
about how socket can differentiate for multiple connections. It is mentioned that based on the Client connection's IP and PORT detected on the socket server and the connection is made unique for each connection. Client PORT will differ for each connection. I read that, we can call getpeername
to get the client connection's IP and PORT on the socket server end. But, i couldn't see any such API available in Java and then found the below one.
ServerSocket server = new ServerSocket(8080);
System.out.println("server.getInetAddress();: " + server.getInetAddress());
But, It is returning always server.getInetAddress():- 0.0.0.0/0.0.0.0
when the mobile client (Simulator) is calling this socket.
Could someone advise me, why I'm not getting the client's IP and PORT using the above InetAddress API on the socket end? Or Do I need to use different approach?
Upvotes: 3
Views: 26703
Reputation: 86774
When the server accept
s a connection you get a Socket
that represents that connection. Socket
has a getInetAddress()
method that will return the remote's address, and a corresponding getPort()
method that will return the remote's port.
For example:
Socket newSock = s.accept();
InetAddress addr = s.getInetAddress();
int port = s.getPort();
Upvotes: 10