File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes how to get Client Ip address? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "how to get Client Ip address?" Watch "how to get Client Ip address?" New topic
Author

how to get Client Ip address?

muneeb ifkaar
Greenhorn

Joined: Jan 05, 2012
Posts: 8
Hello. i want to know that how can we know the ip address of client on server side in socket (java). i.e 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 Db in different tables. How can i Differentiate between client A and B request.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

muneeb ifkaar wrote:Hello. i want to know that how can we know the ip address of client on server side in socket (java). i.e 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 Db in different tables. How can i Differentiate between client A and B request.


Assuming that you are using TCP, your "server", which is using a ServerSocket should return different Socket objects for each client, and hence, should be easy for you to differentiate the two clients.

However, to answer you question -- with the data Socket object, you can get the InetAddress object, which can be used to get the IP address. A word of warning though, this address may be incorrect if the clients are going through a firewall or router that is going through network address translation.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
muneeb ifkaar
Greenhorn

Joined: Jan 05, 2012
Posts: 8
can you please write a sample code for me

Thanks
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

muneeb ifkaar wrote:can you please write a sample code for me

The Ranch is NotACodeMill
Did you take a look at the API for the class which Henry recommended? Do you see any method there which might help you?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
muneeb ifkaar
Greenhorn

Joined: Jan 05, 2012
Posts: 8
yeah i'did. i find some methods whic i think is helpful for me, ie. getHostAdrress() , getByname(), getByAddress(), but i dont know know how to use these methods to get my desired result.
Akhilesh Trivedi
Ranch Hand

Joined: Jun 22, 2005
Posts: 1527
muneeb ifkaar wrote:...but i dont know know how to use these methods.


Static methods can be invoked with its "classname dot method name". For ex. almost all methods in Math class are static, i.e. you can invoke them using "Math.methodName()"

Non-static method require an object before you can place a dot (.) to invoke them.

If method signature has parameters, then calling it requires proper arguments to be passed in its call statement.


Keep Smiling Always — My life is smoother when running silent. -paul
[FAQs] [Certification Guides] [The Linux Documentation Project]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to get Client Ip address?