Note that there are some explanatory texts on larger screens.

plurals
  1. POBufferedReader.readLine() method Blocking alternative
    primarykey
    data
    text
    <p>Is there any way to make the method readline() (from bufferedReader) block until it has in fact something to read??</p> <p>I'm creating an client/server application socketBased and I have this</p> <p>The <strong>Server</strong> reads and then writes. The <strong>Client</strong> writes and then reads.. this is the communication between client/server (based on sockets)</p> <p>The only problem is that on the server I'm reading with bufferedReader.readLine() which isn't a blocking method. I have already tried replacing the bufferedReader.readLine() by dataInputstream().read() (handle all the byte stuffs and getting out when -1 is received) and it isn't working either.</p> <p>The problem in all this is that the server is expecting to read but do not blocks. the client is only going to write to the server (through a socket) when the user press a button (so the server must be waiting for reading and it is not)</p> <p>On the <strong>server</strong> side i have this:</p> <pre><code> BufferedReader READ = new BufferedReader(new InputStreamReader(skt.getInputStream())); BufferedWriter WRITE = new BufferedWriter(new OutputStreamWriter(skt.getOutputStream())); FileInputStream fstream = new FileInputStream(file); DataInputStream in = new DataInputStream(fstream); boolean FLAG = true; String s = new String("#$"); while (FLAG) { if (skt == null) FLAG = false; // READING FROM THE CLIENT while (!READ.ready()){System.out.println("Blocking")}; while ((s = READ.readLine()) != null) { System.out.println("RECEIVING: " + s); System.out.println("$$$"); } System.out.print("hi"); // READING FROM THE FILE AND SENDING TO THE CLIENT //WRITTING TO THE CLIENT while ((strLine = in.readLine()) != null) { System.out.println("SENDING: " + strLine); strLine += "\n"; WRITE.write(strLine); WRITE.flush(); strLine = new String(); } } </code></pre> <p>On the <strong>Client</strong> side i have this:</p> <pre><code> //READING FROM THE FILE AND SENDING TO THE SERVER while((strLine = in.readLine()) != null) { strLine += "\n"; soma+=strLine.length(); WRITE.write(strLine); WRITE.flush(); TextArea1.append(strLine); ProgressBar.setValue(soma); ProgressBar.repaint(); }; in.close(); ProgressBar.setValue(0); soma=0; //READING FROM THE SERVER while((strLine=READ.readLine()) != null) { strLine+="\n"; soma+=strLine.length(); TextArea2.append(strLine); strLine = new String(); ProgressBar.setValue(soma); ProgressBar.repaint(); } in.close(); fstream.close(); WRITE.close(); READ.close(); Skt.close(); </code></pre> <p><strong>NOTE:</strong> Skt is a socket connecting client to server.</p> <p>the problem here is that. When the client connects to the server, the server expects to read from the socket right after the connection is established.. however the code on the client (writing to the server) is only expected to run when the user presses a button.. So, the server must be blocked until the client in fact writes something to the socket which is not (the server is not blocking). :-/ hope to make my self more clear this time.. Sorry</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload