I'm working with a Java application that uses Socket objects for communication and I'm seeing something strange, that I hope someone can help me with.
The system consists of a process that listens for connections from other processes. The client processes create a Socket object and bind it to a specific port number so that the server process can identify the client on connection. The code looks like this:
This code works fine if hostname is not set to "localhost". If it is set to any hostname on the system, or any IP address (including 127.0.0.1), it works. If it is set to "localhost" the connect() call gets a ConnectionRefused exception.
If the bind() call is commented out, then the code always works even if hostname is set to "localhost", but then the server process can't identify the client on connection.
Does anyone know why this code is failing and what I can do to fix it?