I have a java nio server with ip address 192.168.0.1 listening on port 80. I configured this server to bind to multiple domain/host names, let's say server1, server2, server3 are all resolved as 192.168.0.1.
Now I'm hoping if the client connects to server1, on the server side, I can recognize that the client is connecting to server1 instead of other names or direct ip address, so that I can do something special, and if the client is connecting to server2, I can recognize that the client is requesting server2, so I can do something else special, and so on.
Now the question is: seems on the server side, I cannot easily get the correct information which server name my clients are connecting to.
I have the follow code:
ServerSocketChannel ssChannel = (ServerSocketChannel) selKey.channel();
String boundHost = sChannel.socket().getLocalAddress().getHostName();
But it does not work, the boundHost is always the server1 even if clients connect to server2 or server3.
You cannot obtain this kind of information from sockets: sockets don't know about host names, only IP addresses.
If you take the example of HTTP, what distinguishes a host from the other one is the Host
header in the HTTP header.
You can therefore only distinguish different servers if the protocol you use has the hostname information in it.