Socket EADDRINUSE (Address already in use)

后端 未结 4 1658
野趣味
野趣味 2021-02-13 14:13

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

4条回答
  •  死守一世寂寞
    2021-02-13 14:35

    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!

提交回复
热议问题