I have a Java program that was developed long ago. It's even in production now.
We basically try to create a socket as follows:
try {
portnum = 4000;
SSLContext sslctxt;
SSLServerSocket serverSocket = (SSLServerSocket) sslctxt.getServerSocketFactory().createServerSocket(portnum);
} catch (IOException e)
{
//print exception
}
However, recently, when we deployed this on a Linux machine, we are always seeing the following exception thrown by the above code:
java.net.BindException: Address already in use (Bind failed)
I then did the following:
- Brought down all processes.
- Ran
lsof -I:4000
and made sure there are no processes running on this port. - I then started the above Java program.
- Now,
lsof -I:4000
gives a PID and I confirmed that this PID belongs to the Java program I started in step 3. Basically it means that the Java program is able to open a socket on this port number. - However, when I check the logs, I see that it is throwing the BindException I mentioned above.
What is surprising is that I have run this on multiple Linux machines before and never seen this happen. Any inputs on how I can debug this?
Thanks, Om