屏幕关闭时,SSL套接字读取超时暂停

时间:2017-11-09 13:52:48

标签: java android sockets ssl

我设置了从SSLSocket 2分钟读取的超时时间。但是,当电话的屏幕关闭时,java.net.SocketTimeoutException: Read timed不会出现。当我打开屏幕时,超时开始工作。 这是接收方法代码:

public byte[] recieve() {
byte[] result = null;

try {
ByteBuffer bb = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
mSocket.setSoTimeout(120000);
InputStream socketOutStream = mSocket.getInputStream();
Log.d("xxx", "Waiting response's length...");
socketOutStream.read(bb.array(), 0, 4);
int length = bb.getInt();
Log.d("xxx", String.format("  Response length: %d bytes", length));
result = new byte[length];
Log.d("xxx", String.format("Waiting response..", length));
int bytesRead = socketOutStream.read(result);
if (bytesRead == length) {
    Log.d("xxx", String.format("  Bytes actually read: %d", length));
    return result;
}
else if (bytesRead != 0 && bytesRead < length) {
    length -= bytesRead;
    while (length > 0) {
        int bytesActuallyRead = socketOutStream.read(result, bytesRead, length);
        length -= bytesActuallyRead;
        bytesRead += bytesActuallyRead;
    }
    return result;
}

} catch(IOException e) {
e.printStackTrace();
Log.e("xxx", String.format("Socket error: %s", e.getMessage()));
}

return null;
}

我的应用程序应该在屏幕关闭时工作。据我了解,方法read正在阻止。我可以自定义超时吗?我该如何解决这个问题呢?

0 个答案:

没有答案