简体   繁体   中英

Socket always time out with Android Libraries

Socket in Android won't connect even when on server (Java Server) with ServerSocket.Accpet() called before, the connection from client (Android Device) is Received and accepted but the client will never know and keeps timing out .

Here's the Server Code Which is Working on iOS with CocoaAsync Library and running on an Open Port and "socket accepted from .." is printed when client is trying to connect , so its connected and Request handler is just a Class to handle Readings from each client , after accepting one , it will loop again and wait on serverSocket.Accpet() for another client :

ServerSocket serverSocket = null;
    Socket socket = null;
    serverSocket = new ServerSocket(PORT);
    logger.info("started socket server on 127.0.0.1:" + PORT);
    while (true) {
        try {
            logger.info("waiting to receive connection ...");
            socket = serverSocket.accept();
            logger.info("socket accepted from " + socket.getInetAddress().getHostAddress());
        } catch (IOException e) {
            System.out.println("I/O error: " + e);
        }
        // new thread for a client
        RequestHandler t = new RequestHandler(socket);
        t.start();
    }

Android Side :

on manifest <uses-permission android:name="android.permission.INTERNET" />

Using Socket-io Keeps Printing timeout , connection error , reconnect , ...

    IO.Options options = new IO.Options();
    options.forceNew = true;
    options.transports = new String[]{WebSocket.NAME};


    try {
        socket = IO.socket(IP,options); //ip with port
        socket.connect();
        socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_CONNECT", API.class);
            }

        }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_DISCONNECT", API.class);
            }

        }).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_CONNECT_ERROR", API.class);

            }
        }).on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_CONNECT_TIMEOUT", API.class);
            }
        }).on(Socket.EVENT_RECONNECT_ERROR, new Emitter.Listener() {
                    @Override
                    public void call(Object... args) {
                        Utils.Log("Socket EVENT_RECONNECT_ERROR", API.class);
                    }
                })
        .on(Socket.EVENT_RECONNECT_FAILED, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_RECONNECT_FAILED", API.class);
            }
        }).on(Socket.EVENT_RECONNECTING, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_RECONNECTING", API.class);
            }
        }).on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_CONNECT_TIMEOUT", API.class);
            }
        }).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                Utils.Log("Socket EVENT_MESSAGE", API.class);
            }
        });
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

Using AsyncAndroid getting timeout too,

AsyncHttpClient.getDefaultInstance().websocket(IP, "my-protocol", new AsyncHttpClient.WebSocketConnectCallback() {
        @Override
        public void onCompleted(Exception ex, WebSocket webSocket) {
            if (ex != null) {
                ex.printStackTrace();
                return;
            }
            webSocket.setEndCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    Utils.Log("Socket EndCallback", API.class);
                }
            });
            webSocket.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    Utils.Log("Socket ClosedCallback", API.class);
                }
            });
            webSocket.setStringCallback(new WebSocket.StringCallback() {
                public void onStringAvailable(String s) {
                    Utils.Log("Socket setStringCallback", API.class);
                }
            });
            webSocket.setDataCallback(new DataCallback() {
                public void onDataAvailable(DataEmitter emitter, ByteBufferList byteBufferList) {
                    Utils.Log("Socket DataCallback", API.class);
                    byteBufferList.recycle();
                }
            });
        }
    });

, tried rxWebSocket, OkSocket ,nv-websocket-client and some other libraries with the same result keep in mind that I can connect to the server with Java.net simple socket and its working with iOS too.

Testing on Android 8.1 Device and Emulator Thanks in Advance .

UPDATE : I came up with 2 solutions with 2 Libraries and will post them later

this is the code I came up with using AsyncAndroid , where server,currentSocket,isConnected,isConnecting,sessionReloaded(you might not need it) are static variables to send a request I simply call connectSocket(..) if the socket is connected the "currentSocket" variable is returned so it can be written to and the read process is handled at DataCallBack , or it will add the request or whatever you want to write to the socket to a pendingList and try to connect and then write all pendings . (in my case the first thing to write after connecting is session so you might ignore this pattern and you might have your own policy about a request being stuck (pending) behind a connecting socket to do what you want and this is simply the pattern I used but other than that is a common thing)

public static void connectSocket(onSocketConnectionListener listener) {
    if (server == null) {
        server = new AsyncServer();
    }
    if (server.isRunning()) {
        if (listener != null)
            listener.connected(currentSocket);
        return;
    }
    ConnectCallback socketCallBack = new ConnectCallback() {
        @Override
        public void onConnectCompleted(Exception ex, AsyncSocket socket) {
            isConnecting = false;
            if (ex != null) {
                server.stop();
                sessionReloaded = false;
                Utils.Log("Exception = " + ex.getCause() + " , Server Running State = " + server.isRunning(), API.class);
                A.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        new android.os.Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                connectSocket(null);
                            }
                        }, 3000);
                    }
                });
                return;
            }
            Utils.Log("Server Running State = " + server.isRunning(), API.class);
            isConnected = true;
            currentSocket = socket;
            triggerConnections();
            socket.setDataCallback(new DataCallback() {
                @Override
                public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) {
                    byte[] bytes = bb.getAllByteArray();
                    String[] content = Utils.splitJson(new String(bytes));
                    for (int i = 0; i < content.length; i++) {
                        Utils.Log("SocketDataReceived = " + content[i], API.class);
                        handleData(content[i]);
                    }
                    bb.recycle();
                }
            });
            socket.setClosedCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    if (ex != null) {
                        Utils.Log("Closed CallBack With Exception = " + ex.getCause(), API.class);
                    } else {
                        Utils.Log("Closed CallBack", API.class);
                    }
                    sessionReloaded = false;
                    isConnected = false;
                    server.stop();
                    triggerDisconnections();
                    A.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            new android.os.Handler().postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    connectSocket(new onSocketConnectionListener() {
                                        @Override
                                        public void connected(AsyncSocket socket) {
                                        }
                                    });
                                    Utils.Log("Closed CallBack", API.class);
                                }
                            }, 3000);
                        }
                    });
                }
            });
            socket.setEndCallback(new CompletedCallback() {
                @Override
                public void onCompleted(Exception ex) {
                    if (ex != null) {
                        Utils.Log("End CallBack With Exception = " + ex.getCause(), API.class);
                    } else {
                        Utils.Log("End CallBack", API.class);
                    }
                    sessionReloaded = false;
                    isConnected = false;
                    server.stop();

                }
            });
            socket.setWriteableCallback(new WritableCallback() {
                @Override
                public void onWriteable() {
                    Utils.Log("Write CallBack", API.class);
                }
            });

            User user = MDatabase.getUser();
            String session = MDatabase.getSession();
            if (sessionNeedsReload()) {
                reloadSession(user.mobile, session);
            } else {
                doPendingRequests();
            }
        }
    };

    synchronized (isConnecting) {
        if (!isConnecting) {
            isConnecting = true;
            Utils.Log("Socket Connecting ...", API.class);
            server.connectSocket(IP, PORT, socketCallBack);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM