Is there any way to make the method readline() (from bufferedReader) block until it has in fact something to read??
I’m creating an client/server application socketBased and I have this
The Server reads and then writes. The Client writes and then reads.. this is the communication between client/server (based on sockets)
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.
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)
On the server side i have this:
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();
}
}
On the Client side i have this:
//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();
NOTE: Skt is a socket connecting client to server.
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
BufferedReader.readLine() always blocks until it finds
'\n'.If you can give examples of data you’re sending around, I might help you further.