1

我正在使用 socketConnection 在黑莓曲线 9300 中编写程序。当我尝试读取输入流时,出现流关闭/错误套接字 id 错误。你能建议我可能是什么问题。

String url = Constants.SOCKET + serverIp + Constants.COLON + serverPort + UserInfo.getConnectionType();

//connection open
socketConne`enter code here`ction = (SocketConnection) Connector.open(url, Connector.READ_WRITE,true);

socketConnection.setSocketOption(SocketConnection.DELAY, 0);
socketConnection.setSocketOption(SocketConnection.KEEPALIVE, 1);
socketConnection.setSocketOption(SocketConnection.RCVBUF, 16834);
socketConnection.setSocketOption(SocketConnection.LINGER, 10);

outputStream = socketConnection.openDataOutputStream();

//Output stream deligated to Sender Object to send the command
sender = new Sender(outputStream);
sender.send(command);
inputStream = socketConnection.openDataInputStream();

//read inputstream
while (tobeRead < toRead && retryCount < 5) {
    try {
        if ((toRead - tobeRead) > 8192) {
            readBufferLength = 8192;
        } else {
            readBufferLength = (toRead - tobeRead);
        }
        buffer = new byte[readBufferLength + 1];

        bytesRead = inputStream.read(buffer, 0, readBufferLength);
        sb.append(new String(buffer, 0, bytesRead));
        tobeRead += bytesRead;
        totalBytesRead += bytesRead;
    } catch (Exception cce) {
    }
}

说如果要读取的字节数是 5192。我说 2123 字节然后它迭代循环,下一次读取它失败。我在单独的线程中运行它。我使用 WIFI 网络进行连接

4

0 回答 0