koush / AndroidAsync

Asynchronous socket, http(s) (client+server) and websocket library for android. Based on nio, not threads.
Other
7.52k stars 1.56k forks source link

java.net.BindException: Cannot assign requested address #693

Open Smartelic opened 3 years ago

Smartelic commented 3 years ago

AsyncServer.getDefault().listen(host, port, new ListenCallback() is giving bind exception all the time.

Here is the crash log,

--------- beginning of crash
2021-02-07 02:03:05.832 11083-11134/com.github.renewb.androidasyncsocketexamples E/AndroidRuntime: FATAL EXCEPTION: AsyncServer
    Process: com.github.renewb.androidasyncsocketexamples, PID: 11083
    java.lang.RuntimeException: java.net.BindException: Cannot assign requested address
        at com.github.reneweb.androidasyncsocketexamples.tcp.Server$1.onCompleted(Server.java:47)
        at com.koushikdutta.async.AsyncServer$3.run(AsyncServer.java:368)
        at com.koushikdutta.async.AsyncServer.lambda$run$1(AsyncServer.java:210)
        at com.koushikdutta.async.-$$Lambda$AsyncServer$Wv_yQXhLxsNsbvajGAFlXa0mU2Y.run(Unknown Source:4)
        at com.koushikdutta.async.AsyncServer$Scheduled.run(AsyncServer.java:236)
        at com.koushikdutta.async.AsyncServer.lockAndRunQueue(AsyncServer.java:788)
        at com.koushikdutta.async.AsyncServer.runLoop(AsyncServer.java:806)
        at com.koushikdutta.async.AsyncServer.run(AsyncServer.java:717)
        at com.koushikdutta.async.AsyncServer.access$800(AsyncServer.java:46)
        at com.koushikdutta.async.AsyncServer$11.run(AsyncServer.java:671)
     Caused by: java.net.BindException: Cannot assign requested address
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:442)
        at sun.nio.ch.Net.bind(Net.java:434)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)

It's working fine, when i tried with val socket = Socket(serverAddress, PORT) .

Here is the code snippet that i am using

AsyncServer.getDefault().listen(host, port, new ListenCallback() {

            @Override
            public void onAccepted(final AsyncSocket socket) {
                handleAccept(socket);
            }

            @Override
            public void onListening(AsyncServerSocket socket) {
                System.out.println("[Server] Server started listening for connections");
            }

            @Override
            public void onCompleted(Exception ex) {
                if(ex != null) throw new RuntimeException(ex);
                System.out.println("[Server] Successfully shutdown server");
            }
        });

Is there anything i am missing?