I am doing socket programming. I took reference from below link:
http://examples.javacodegeeks.com/android/core/socket-core/android-socket-example/
Below is deta
While other answers pointed out the importance of setReuseAddress(true)
, another problem that could arise is to construct the ServerSocket twice and call bind with the same parameters. For example if you call twice the code run()
of the question, serverSocket
will be assigned to a new instance of the ServerSocket class, but the old one is still living until garbage collected. Now constructing with the port value as parameter equals to bind the ServerSocket object, and you are going to end up with two ServerSocket bound to the same address, which is forbidden hence the exception. So build your serverSocket with your chosen port only once!