When receiving data using readLine(), even though I put a "\n" at the end of the message using the .flush when sending the message, the while loop that reads my message still blocks. Only when closing the socket connection, it leaves the loop.
Here's the client code :
bos = new BufferedOutputStream(socket.
getOutputStream());
bis = new BufferedInputStream(socket.
getInputStream());
osw = new OutputStreamWriter(bos, "UTF-8");
osw.write(REG_CMD + "\n");
osw.flush();
isr = new InputStreamReader(bis, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String response = "";
String line;
while((line = br.readLine()) != null){
response += line;
}
and the server's code:
BufferedInputStream is;
BufferedOutputStream os;
is = new BufferedInputStream(connection.getInputStream());
os = new BufferedOutputStream(connection.getOutputStream());
isr = new InputStreamReader(is);
String query= "";
String line;
while((line = br.readLine()) != null){
query+= line;
}
String response = executeMyQuery(query);
osw = new OutputStreamWriter(os, "UTF-8");
osw.write(returnCode + "\n");
osw.flush();
My code blocks at the server while loop. Thanks.
I tried a lot of solutions but the only one I found which wasn't blocking execution was:
The
inStream.ready()
returns false if the nextreadLine()
call will block the execution.if you want to get what's in the socket without being forced to close it simply use ObjectInputStream and ObjectOutputStream ..
Example:
The BufferedReader will keep on reading the input until it reaches the end (end of file or stream or source etc). In this case, the 'end' is the closing of the socket. So as long as the Socket connection is open, your loop will run, and the BufferedReader will just wait for more input, looping each time a '\n' is reached.
This happens because the InputStream is not ready to be red, so it blocks on in.readLine() . Please try this :
Of course you have to control the exitCondition .
An other option can be the use of nio package, which allows asynchronised (not blocking) reading but it depend on your need.
readline() and read() will be blocked while socket doesn't close. So you should close socket:
rather than Socket.close();
This is because of the condition in the while-loop:
while((line = br.readLine()) != null)
you read a line on every iteration and leve the loop if readLine returns
null
.readLine returns only null, if eof is reached (= socked is closed) and returns a String if a '\n' is read.
if you want to exit the loop on readLine, you can omit the whole while-loop und just do:
line = br.readLine()