There is a simple Java server on sockets. The server runs on the local machine, so you need to use the localhost address to access it. How to make access to the server not by the name localhost , but by some other name, for example, example.com
2 answers
Option 1. Access to the server is needed only from the local machine. In this case, it is enough to make changes to the %windir%\System32\drivers\etc\hosts by typing the line
127.0.0.1 example.com Option 2. Access to the server is needed from several machines from the local network. In this case, it is necessary for the server to listen not only to the address 127.0.0.1 , but also the address specified in the settings of the network card. However, you can not bother and listen to all addresses ( 0.0.0.0 ). On other machines, you also need to make changes to the hosts .
Option 3. You need access from the Internet. This is the most difficult case. First you need a "white" static IP address. This address should be either at the server or at any device capable of "forwarding" the necessary ports to your server. Next, we buy a domain name example.com from any registrar and enter the so-called A-record in the settings of this domain name, which will contain the “white” static IP address.
- How in the second variant to make, that the server listened not only
localhost? - jisecayeyo - To listen to all addresses, just pass
nullas anInetAddress. Or just use the constructor without specifying the address:new ServerSocket(port). - Roman
If I understand correctly, in the hosts file you should have the following:
127.0.0.1 localhost add the same line and change localhost for what you need
- @kff thanks for the comment. - Artem Konovalov