I have written a simple Java application that interacts with multiple instances of itself using sockets. The first instance automatically takes on the role of the server, listen
Use the three parameter constructor of the ServerSocket class to specify the IP address as well that the server it should listen on. That way you can restrict the server to listen only on 127.0.0.1, unlike the default of 0.0.0.0. See this related Stack Overflow question, for more details.
It is preferable to use InetAddress.getByName(null)
to obtain the local address.