how to get client ip address?

牧云@^-^@ 提交于 2019-12-17 20:28:07

问题


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.?


回答1:


Each socket has a getInetAddress() method which returns the IP address of the client connected to it.




回答2:


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.



来源:https://stackoverflow.com/questions/8959466/how-to-get-client-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!