Reputation: 923
I am trying to get the domain a user is connection to (e.g. test.somesite.com) from a Socket, is this possible? The reason I would like this is to redirect users based on where they came from. I get the socket from running serverSocket.accept() in another thread
I have tried:
clientSocket.getLocalAddress().getHostAddress();
clientSocket.getHostAddress().toString();
They both just print my own local/internal IP address. How can I get the domain they came through from that?
Edit: I think this could be possible because for example, in an Apache web server you can do this in your httpd.conf file:
<VirtualHost *:80>
ServerName something.something.com
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
This will listen on port 80, check if the url they came from is something.something.com, and if it is it will redirect to port 80. Maybe I am completely wrong because I'm new to networking in Java but please let me know if you have any hints, even if I have to rewrite my code its fine.
Edit 2: I found out this is impossible because the target application resolves the domain name to an IP address before it connects. Thanks for all your help though
Keir
Upvotes: 0
Views: 2056
Reputation: 598424
Try this:
clientSocket.getInetAddress().getHostName()
Update: Alternatively, try this:
((InetSocketAddress)clientSocket.getRemoteSocketAddress()).getAddress().getHostName()
Upvotes: 1
Reputation: 1
I don't think you can get the domain without a DNS, but you could use the getRemoteSocketAddress() method from clientSocket to get the client IP address.
clientSocket.getRemoteSocketAddress();
Upvotes: 0