Socket EADDRINUSE (Address already in use)

后端 未结 4 1664
野趣味
野趣味 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:20

    You have to call setReuseAddress(true) before the socket is bound to the port. You are calling it after, because you are passing the port to the constructor, which will bind the socket immediately.

    Try this instead:

    serverSocket = new ServerSocket(); // <-- create an unbound socket first
    serverSocket.setReuseAddress(true);
    serverSocket.bind(new InetSocketAddress(SERVER_PORT)); // <-- now bind it
    

提交回复
热议问题