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

}

}

[2603 byte] By [J_MARTINEZa] at [2007-11-26 18:25:21]
# 1
Server seems to read from the socket, but the client does not write anything.
BIJ001a at 2007-7-9 5:59:25 > top of Java-index,Core,Core APIs...
# 2

> Client code:

> while((read=br.readLine())!=null){

> System.out.println(read);

> }

This loop will only end when the other end closes the socket, or calls Socket.shutdownOutput(). As your server hasn't done either of these things it won't.

ejpa at 2007-7-9 5:59:25 > top of Java-index,Core,Core APIs...
# 3

You are right but with this program the loop ends. As you see, the first class, the Server, writes to the socket one text file. The second class, the client, reads the text file in his socket written by the server and writes it to a file in his machine.

NOTE: The loop in the client ends and the server doesnt make any close() socket or shutdownOutput() .

public class ServidorSocketFicheromio {

public ServidorSocketFicheromio() {

try{

ServerSocket servidor= new ServerSocket(2000);

System.out.println("servidor iniciado");

Socket cliente=servidor.accept();

System.out.println("cliente conectado");

OutputStream os=cliente.getOutputStream();

PrintWriter pw= new PrintWriter(os);

File f = new File("c:\\curso java\\DUDAS.TXT");

FileReader fr= new FileReader(f);

BufferedReader br= new BufferedReader(fr);

String leido;

while((leido=br.readLine())!=null){

pw.println(leido);

}

pw.flush();

}catch (IOException e) {

}

}

/**

* @param args

*/

public static void main(String[] args) {

new ServidorSocketFicheromio();

}

}

public class ClienteSocketFicheromio {

public ClienteSocketFicheromio() {

try{

Socket cliente= new Socket("localhost",2000);

File f = new File("G:\\pepe.txt");

FileWriter fw= new FileWriter(f);

PrintWriter pw= new PrintWriter(fw);

InputStream is=cliente.getInputStream();

InputStreamReader isr= new InputStreamReader(is);

BufferedReader br= new BufferedReader(isr);

String leido;

while((leido=br.readLine())!=null){

pw.println(leido);

System.out.println(leido);

}

System.out.println("leido");

System.out.println("hola");

pw.flush();

}catch (IOException e) {

}

}

public static void main(String[] args) {

new ClienteSocketFicheromio();/

}

}

J_MARTINEZa at 2007-7-9 5:59:25 > top of Java-index,Core,Core APIs...
# 4

> You are right but with this program the loop ends.

No. If I am right, the loop doesn't end, so 'hola' won't be printed. If the loop does end, I am wrong, the 'hola' line will be printed, and you don't have a problem.

You can't have it both ways.

It isn't being printed, so the loop isn't ending, so I am right, and so is the Javadoc for readLine(), which you don't seem to have understood.

I think you're making the common mistake of thinking that readLine() will return null once the peer stops sending lines. It won't, it will block until another line is sent or the socket is closed or shutdown by the peer.

ejpa at 2007-7-9 5:59:25 > top of Java-index,Core,Core APIs...