Sockets 套接字id错误,流关闭错误+blackberry套接字连接
我正在使用socketConnection在blackberry curve 9300中编写一个程序。当我尝试读取inputstream时,收到流关闭/错误套接字id错误。 你能告诉我有什么问题吗Sockets 套接字id错误,流关闭错误+blackberry套接字连接,sockets,blackberry,connection,Sockets,Blackberry,Connection,我正在使用socketConnection在blackberry curve 9300中编写一个程序。当我尝试读取inputstream时,收到流关闭/错误套接字id错误。 你能告诉我有什么问题吗 String url = Constants.SOCKET + serverIp + Constants.COLON + serverPort + UserInfo.getConnectionType(); //connection open socketConne`enter code here`
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网络进行连接您如何知道需要读取5192字节?你从哪里得到tobeRead值?我不明白。您在第一次迭代期间读取了完整的缓冲区吗。或者你把它加满了一部分?还有小优化-无需每次创建缓冲区。创建一次并重复使用每次迭代。从服务器上,我获得流的大小\n和实际流的大小,因此我读取大小,创建大小为8192的缓冲区并循环,直到获得完整的数据。如果尺寸小于8192,我可以一次性阅读。黑莓设备是否存在读取完整数据的问题。同样的事情在模拟器中也可以很好地工作