1

I have written a program that makes two threads both containing ServerSocket object listening to two different ports and waiting to accept() in while loops, after I run it, starting one of these threads causes no Exception, but when it gets to starting the other, I get java.net.BindException as below:

java.net.BindException: Address already in use: JVM_Bind
    at java.net.DualStackPlainSocketImpl.bind0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketBind(Unknown Source)
    at java.net.AbstractPlainSocketImpl.bind(Unknown Source)
    at java.net.PlainSocketImpl.bind(Unknown Source)
    at java.net.ServerSocket.bind(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at java.net.ServerSocket.<init>(Unknown Source)
    at ListeningThread.run(ListeningThread.java:18)

I used different ports for both many times, but didn't work, please help me fix this problem. I appreciate your help.

Rafal G.
  • 4,252
  • 1
  • 25
  • 41
Jihad Mehdi
  • 65
  • 1
  • 9
  • 1
    Got any code...? I'd like to run it myself and see why this is happening. – Water May 23 '15 at 20:52
  • 3
    You get this exception if you have two processes or threads listening to the **same** port. Are you sure they are using different ports? And that there is no other process listening to those ports? Show your code. – Jesper May 23 '15 at 20:52
  • Or, another program is listening on one of the ports ... Keep in mind, that there may be program listening on any address (0.0.0.0) that would prohibit you from binding to a specific address and the same port. – Fox May 23 '15 at 20:53
  • It's either a bug/typo and you *are* trying to bind the same port, or there is another process that is already listening on the second port. Use `netstat -na` to check what ports are in use. – Nikolai Fetissov May 23 '15 at 20:53
  • @Water the program is a little complex to write all the code here, but in general its implementation equally like [this](http://stackoverflow.com/questions/21118276/listening-to-two-ports-simultaneously-in-java-server-using-multithreading) – Jihad Mehdi May 23 '15 at 21:02
  • I already know that the main cause for this should be two processes or threads are listening to the same port, but in this case I don't know why it's not. – Jihad Mehdi May 23 '15 at 21:13
  • 1
    You could at least log the socket name you want to bind to .. – Ingo May 23 '15 at 21:48
  • @user3120117 I don't think anyone can help until you provide an [mcve](http://stackoverflow.com/help/mcve). – Water May 23 '15 at 22:26
  • If you are on Linux, try `socket.setReuseAddress(true)`. but don't do it on Windows. – ZhongYu May 23 '15 at 22:55

2 Answers2

0

I used different ports for both many times, but didn't work, please help me fix this problem.

Failing to set SO_REUSEADDR (using socket.setReuseAddress(true)) can cause binding to fail if the port was used in the recent past, even if no process is currently using it.

The other possibility is a logic error in your code that causes it to use the same port for both threads. In this case, you should add a trace that prints the port about to be bound.

Atsby
  • 2,277
  • 12
  • 14
0

Thanks for your replies, I fixed the the problem just by adding Thread.sleep() between starting the two threads, even postponing by (1 millisecond), I still can't figure out what was the problem.

new ListeningThread(this, 6666).start();
Thread.sleep(1);
new ListeningThread(this, 7777).start();
Jihad Mehdi
  • 65
  • 1
  • 9