Madhusoodan P
Madhusoodan P

Reputation: 667

Different IP Addresses from the same machine

I have a client-server setup. Where client connects to the server and asks which program to execute next. Server after getting a request checks for the hostname of the other end of the socket using below chunk of code which returns "127.0.0.1"

socket.getInetAddress().getCanonicalHostName();

After getting the program name the client creates a process which also tries to connect to the server but this time the above call returns different address. "mypc.foo.com" masking the domain name here

This behavior is bugging me as I am unable to lookup the hashmap where I store all the process details grouped by the machine ip.

Is it a bug in java lookup implementation or am I missing something. Please suggest some other way to do this lookup

Upvotes: 0

Views: 1296

Answers (1)

Veselin Davidov
Veselin Davidov

Reputation: 7071

I beleive socket.getInetAddress() returns your own address so basically always 127.0.0.1. Try using socket.getRemoteSocketAddress() - to get the other party's ip address.

Using IP address to match distinct users is generally a bad idea though. First of all they can be in some network or behind firewall and you can get requests from multiple clients coming from the same IP address. Also you are not guaranteed that it is static or dynamic IP. And also if your application is running in some strange network setup with strange routing you might end up getting all requests from the router IP address.

The better design would be to share some token between the server and the client and use that for identification. Does it sound familiar? For example http sessions are done like that ;)

Upvotes: 1

Related Questions