Problem with the socket and the standard output stream
Hy, I have a little problem with a socket program. It has the server and the client. The problem is that the client at one point in the program, cannot print messages in the console.
My program does the next: the server waits connections, when a client connects to it, the server gets outputstream to the socket and writes two strings on it. Meanwhile, the client gets the inputstream to the socket and reads on it with a loop the two strings written by the server . The strings are printed by the client in the console. The problem starts here; once the read strings are printed ,I mean, after the loop, there are other System.out.println in the client but the console doesnt print anything . It curious that only when I comment on the server code the line that says: "br.readLine()" just before the catch, the client prints all the System.out.println after the loop but why?
Here is the code:
Server code:
public class MyServerSocket {
public MyServerSocket() {
try{
ServerSocket server= new ServerSocket(2000);
System.out.println("servidor iniciado");
Socket client=server.accept();
System.out.println("Client connected");
OutputStream os=client.getOutputStream();
PrintWriter pw= new PrintWriter(os);
String cadena1="cadena1";
String cadena2="cadena2";
pw.println(cadena1);
pw.println(cadena2);
pw.flush();
InputStream is=client.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br= new BufferedReader(isr);
br.readLine(); //If a comment this line, the client prints after the loop, all the System.out....
}
catch (IOException e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
new MyServerSocket
}
}
Client code:
public class MyClientSocket {
public MyClientSocket () {
try{
Socket client= new Socket("localhost",2000);
InputStream is=client.getInputStream();
InputStreamReader isr= new InputStreamReader(is);
BufferedReader br= new BufferedReader(isr);
String read;
while((read=br.readLine())!=null){
System.out.println(read);
}
//These messages are not printed unless I comment the line I talked about in the server code
System.out.println("leido");
System.out.println("hola");
}catch (IOException e) {
}
}
public static void main(String[] args) {
new MyClientSocket
}
}

