开发者

Need to have "stable" TCP connection to server

I need to have a "stable" connection to a server.

  1. The client tries to connect to the server every 5 (10, N)-seconds.

  2. After having connected successful开发者_JS百科ly the client receives data from the server.

  3. In case of service interruption (server shutdown, for example), go to step #1.

How I test:

  1. I start the server

  2. I start the client (to be sure that client gets data from the server)

  3. I stop the server

  4. I wait for about 200 client attempts to connect to the server.

  5. I restart the server.

The server sends data, but the client doesn't get it. socket.connect(...) is sucessfull, but socket.getInputStream().read(byte[]) is not: the Thread blocks on input.read(..).

If I uncomment this line:

//socket.setSoTimeout(500);

then input.read(..) throws a TimeoutException.

But the server receives data from the client.

Where is my wrong? Thanks.

Part of client code:

private void initSocket() {
    try {
        if (socket == null || socket.isClosed() == true
                || socket.isConnected() == false) {
            socket = new Socket();
            // socket.setSoTimeout(500);
            InetSocketAddress socketAddress = new InetSocketAddress("192.168.1.3"
                  , 12344);
            notifyDataListener(4);
            socket.connect(socketAddress, 500);
            notifyDataListener(5);
        }
    } catch (Throwable t) {
        System.err.println(t);
    }
}

private void closeSocket() {
    try {
        if (socket != null && socket.isClosed() == false) {
            socket.close();
        }
    } catch (Throwable t) {
        System.err.println(t);
    }
}

private byte[] buffer = new byte[1024];

public void run() {
    while (isActive) {
        try {
            notifyDataListener(1);
            initSocket();
            InputStream input = socket.getInputStream();
            int length = input.read(buffer);
            if (length < 0) {
                throw new EOFException("Was got -1");
            }
            notifyDataListener(2);
        } catch (Throwable t) {
            closeSocket();
            notifyDataListener(3);
            try {
                Thread.sleep(100);
            } catch (InterruptedException ie) {
            }
        }
    }
}

On J2SE the same code works fine. Connection repairs after many wrong attempts. It looks like Android has limit slosts of sockets (FileDescriptior?), takes them, but don't release after.


Your likely running out of file descriptors, i'm sure the limit is much lower on android than on a typical desktop configuration but the specific values will vary.

With the way you've coded this, the socket will hang around until its garbage collected, additionally on some platforms, the OS level sockets do not close instantly but hang around for a period of time to clean up any hanging data.

The first thing you should do is move your socket.close() code to finally {} statements which will free the socket immediately rather than waiting for garbage collection.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜