[ACCEPTED]-Socket input stream hangs on final read. Best way to handle this?-sockets
The problem with just stopping where read 19 would hang is that this can happen in 2 18 cases:
1: server doesn't have any more data 17 to send
2: The server has sent more data, but 16 your client has not received it yet due 15 to network overload.
And you only want to 14 really stop reading in the first case, but 13 you want read to block in the second case.
The 12 way to solve this is to make a transfer 11 protocol(Standard) which allows the server 10 to tell the client how much data it expects 9 to send.
If the server knows the total data 8 size in advance, simply start by sending 7 the total number of bytes in the transfer, and 6 then send the data. That way the client 5 knows when it have received all data.
(Or 4 the server can simply close the connection 3 when done. That way read
should fail, but this 2 only work if you don't need the connection 1 in the future)
I think you will only get -1 when the server 4 decides to stop the conversation, i.e. closes 3 its output stream or closes the socket completely. Else, the 2 stream stays open for potential future incoming 1 data.
I'm not a Android expert, but this seems 1 to work just fine on Android:
try {
telnetClient = new Socket(server, port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (telnetClient.isConnected()) {
try {
InputStream instr = telnetClient.getInputStream();
int buffSize = telnetClient.getReceiveBufferSize();
if (buffSize > 0) {
byte[] buff = new byte[buffSize];
int ret_read = instr.read(buff);
screen.append(new String(buff, 0, ret_read));
}
} catch (IOException e) {
screen.append("Exception while reading socket:" + e.getMessage());
}
}
I've just had this exact same problem. I 3 solved it by setting a timeout straight 2 after creating the socket object... socket.setSoTimeout(10 1 * 1000);
I found this problem to be pretty formidable. I 11 was trying to execute a windows batch command 10 from java and the process hangs every time 9 trying to read the inputstream from the 8 command getting executed.
I could only fix 7 this inelegantly.
a) By looking for a specific 6 line which I know would signal the end
useful 5 information form the stream.
b) Or to start 4 a new Thread that
kills the process 3 at the other end of the socket, in this 2 case for
me IEDriverServer.exe after 1 a wait time.
String[] cmdArray = {"cmd.exe","/c",cmd+ " 2>&1" };
InputStream is = new ProcessBuilder(cmdArray).directory(f).start().getInputStream();
error_buffer = new BufferedReader(new InputStreamReader(is));
int lines=0;
while((line=error_buffer.readLine())!=null){
if(line.startsWith("[INFO] Final Memory: ")) {
break;
}
log.debug("Reading cmd Stream "+line);
buf.append(line);
lines++;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.