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.