• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to get Client Ip address?

 
muneeb ifkaar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21021
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
muneeb ifkaar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please write a sample code for me

Thanks
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
muneeb ifkaar
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic