Hello,
I need help understanding how the readline() method of a BufferedReader object works in Java.
From what I understanding when a BufferedReader object reads from a file using the readline() method it reads the file line by line and when there is nothing left to read the method returns a null (The code labelled Example1 is an illustration of this).
However I don’t understand how the readline() method works when reading from a socket. Please refer to the code labelled as Example 2. A Client program that connects to the server illustrated in Example 2 can send text any time and can wait long periods of time between sending text to the server.
How does the java virtual machine know when the end of the stream related to the socket has been reached so that readline() can return null?
Example 1:
Example 2
Source of Code: Head First Java 2nd edition – Chap 15
I need help understanding how the readline() method of a BufferedReader object works in Java.
From what I understanding when a BufferedReader object reads from a file using the readline() method it reads the file line by line and when there is nothing left to read the method returns a null (The code labelled Example1 is an illustration of this).
However I don’t understand how the readline() method works when reading from a socket. Please refer to the code labelled as Example 2. A Client program that connects to the server illustrated in Example 2 can send text any time and can wait long periods of time between sending text to the server.
How does the java virtual machine know when the end of the stream related to the socket has been reached so that readline() can return null?
Example 1:
Code:
import java.io.*;
class Reader{
public static void main(String[] args){
int lineCount = 0;
try {
File myFile = new File("Reader.java");
FileReader fileReader = new FileReader(myFile);
BufferedReader reader = new BufferedReader(fileReader);
String line = null;
// *** My question is about the next line **/
while( (line = reader.readLine()) != null ) {
System.out.println(++lineCount + " " + line);
}
reader.close();
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println("lineCount = " + lineCount);
}
}
Example 2
Code:
import java.io.*;
import java.net.*;
import java.util.*;
public class VerySimpleChatServer
{
ArrayList clientOutputStreams;
public class ClientHandler implements Runnable {
BufferedReader reader;
Socket sock;
public ClientHandler(Socket clientSOcket) {
try {
sock = clientSOcket;
InputStreamReader isReader = new InputStreamReader(sock.getInputStream());
reader = new BufferedReader(isReader);
} catch (Exception ex) { ex.printStackTrace(); }
}
public void run() {
String message;
try {
// *** My question is about the next line **/
while ((message = reader.readLine()) != null) {
System.out.println("read " + message);
tellEveryone(message);
}
} catch (Exception ex) { ex.printStackTrace(); }
}
}
public static void main(String[] args) {
new VerySimpleChatServer().go();
}
public void go() {
clientOutputStreams = new ArrayList();
try {
ServerSocket serverSock = new ServerSocket(5000);
while(true) {
Socket clientSocket = serverSock.accept();
PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
clientOutputStreams.add(writer);
Thread t = new Thread(new ClientHandler(clientSocket));
t.start();
System.out.println("got a connection");
}
} catch (Exception ex) { ex.printStackTrace(); }
}
public void tellEveryone(String message) {
Iterator it = clientOutputStreams.iterator();
while (it.hasNext()) {
try {
PrintWriter writer = (PrintWriter) it.next();
writer.println(message);
writer.flush();
} catch (Exception ex) { ex.printStackTrace(); }
}
}
}