Is it possible to get the server address used by a TCP client? A client can reach the server using either IP address or host/domain name.
I'm trying to get the domain name using:
ServerSocket ss = new ServerSocket(port);
Socket s = ss.accept();
System.out.println(s.getLocalAddress().getHostName());
System.out.println(s.getLocalAddress().getCanonicalHostName());
but all I get is IP address, always!
In an HTTP server, we can achieve the same using httpServletRequest.getServerName(). It returns IP address if the HTTP client uses IP address and it returns domain name if the HTTP client uses domain name. So I'm quite sure it should be possible at TCP level also.
if you tried your
LocalHostas a server it will probably bring back the name, it might be a DNS problem tho; anyway try youlocalhostas a server and if it worked go to%SystemRoot%\system32\drivers\etc\hostsand define your host names.check this out.
hope this will help.