Reputation:
I want to know that how can we know the IP address of client on server side in socket (java). Suppose there is a server "S" and 2 clients "A" and "B". Client A send me(server) some info, and Client B send me(server) some other info. I have to store that info coming from A and B in database in different tables. How can I Differentiate between client A and B request.?
Upvotes: 2
Views: 7212
Reputation: 719486
The other answer has addressed your core question. I just want to add that there are some circumstances where the IP address you get back does not uniquely identify the true endpoint; i.e. the user's PC.
If two users are using the same system to connect; i.e. it is a multi-user system.
If the user's PC is behind a NAT gateway (because it has a private / site-local IP address), then the IP address you will get will be for the gateway.
It is also for the address to have been spoofed or hijacked in some way ...
There is nothing you can do to detect either of these situations from the server side. You just need to be aware of them, and not rely on the (supposed) client IP address as being definitive.
So ...
How can I Differentiate between client A and B request?
In any of the above circumstances, you cannot distinguish A and B by IP address alone. If you look at the connections to A and B while both connections are alive, they will have different remote port numbers. But those port numbers only last for the duration of the connection. However, next time A or B connects, A could be using the remote port that B was using, or vice versa.
Upvotes: 0
Reputation: 24740
Each socket has a getInetAddress()
method which returns the IP address of the client connected to it.
Upvotes: 8