Socket EADDRINUSE (Address already in use)

后端 未结 4 1663
野趣味
野趣味 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
    
    0 讨论(0)
  • 2021-02-13 14:29

    Try to create the instance of SocketServer outside of the run() method.

    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState)   
        try {
            // create a new instance of an unbound socket first
            serverSocket = new ServerSocket(); 
            } catch (IOException e) {
               e.printStackTrace();
            }
    }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2021-02-13 14:37

    TCP (and probably some other) sockets can't reuse the same port for a period after closing. This is to prevent confusion if there's data on the network from an existing connection. You can override this behavior, but the default is to wait for a period of time before allowing reuse of the port.

    The call to fix this is setReuseAddress(true) on the server socket. But I'm not sure if it needs to be called on the first socket or the second, or both.

    Edit:

    Here's a blog post describing the TCP socket TIME_WAIT state and why it exists: http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html

    0 讨论(0)
提交回复
热议问题