Java Bind Exception

What would cause the TCP socket to throw "java.net.BindException: Address already in use" even if the reuse address is set to true? This only happens if the application restarts quickly. Powered by CentOS 5 Linux.

+2
source share
3 answers

This view explains this:

http://www.beej.us/guide/bgnet/output/html/singlepage/bgnet.html#bind

Sometimes you may notice that you are trying to restart the server and bind () is not executed, stating that "The address is already in use." What does it mean? Well, a little bit of the socket that was plugged in, still hanging in the kernel, and it clogs the port. You can either wait for it to clear (a minute or so), or add code to your program allowing it to reuse the port, for example this

(contains code C)

Basically, in C, you call a function called setsockopt (), and one of the parameters is called SO_REUSEADDR, which allows you to reuse this port.

I found some brief google links that should help you understand how to set the equivalent option in Java:

http://java.sun.com/j2se/1.4.2/docs/guide/net/socketOpt.html

http://java.sun.com/j2se/1.4.2/docs/api/java/net/SocketOptions.html

+3
source

If what you are saying is correct, you should catch this exception in a loop and try again in a few seconds. (You do not need to do this, but I heard about a few unusual things about CentOS)

+2
source

Java Bind Error Exception If any of your port or InetAddress is already in use and you want to use it again. Therefore, free the port, stop the program if it is running. otherwise change the port

Thanks Deepak

+1
source

All Articles