PrintWriter won't write

前提是你 提交于 2019-12-24 08:27:12

问题


I have the following problem. I programmed a simple Echo Server and Echo Client but the problem is that in the loop of the Server, where I read from the Buffered Reader, the programme stuck and it won't write.

    import java.net.*;
import java.util.*;
import java.io.*;
public class SimpleServer {
  public static void main(String[] args) {
    System.out.println("Dies ist ein simpler Echo Server");
    int port = 6000;
    try {
      ServerSocket server = new ServerSocket(port);                       
      //server.setSoTimeout(30000);                                       
      System.out.println("Warte auf anfrage");                            
      Socket client = server.accept();                                   
      InputStream is = client.getInputStream();          
      InputStreamReader isr = new InputStreamReader(is);                  
      BufferedReader reader = new BufferedReader(isr);                   
      String message = null;                                              
      while ((message = reader.readLine())!=null) {                       
        System.out.println("Nachricht vom Client "+message);              
      }
      OutputStream os = client.getOutputStream();                        
      PrintWriter writer = new PrintWriter(os);                          
      System.out.println(message);
      writer.println(message);                                           
      writer.flush();                                                    

      writer.close();                                                    
    } catch(IOException e) {
      e.printStackTrace();
    }  // end of try
  } // end of main
} // end of class SimpleServer

But when i put the .println() and .flush() in the loop everything works great.

import java.net.*;
import java.util.*;
import java.io.*;
public class SimpleServer {
  public static void main(String[] args) {
    System.out.println("Dies ist ein simpler Echo Server");
    int port = 6000;
    try {
      ServerSocket server = new ServerSocket(port);                       
      //server.setSoTimeout(30000);                                       
      System.out.println("Warte auf anfrage");                            
      Socket client = server.accept();                                    
      InputStream is = client.getInputStream();                            
      InputStreamReader isr = new InputStreamReader(is);                 
      BufferedReader reader = new BufferedReader(isr);                        
      OutputStream os = client.getOutputStream();                         
      PrintWriter writer = new PrintWriter(os);                          
      String message;                                                     
      while ((message = reader.readLine())!=null) {                       
        System.out.println("Nachricht vom Client "+message);              
        writer.println(message);                                           
        writer.flush();                                                     
      } 
      writer.close();                                                     
    } catch(IOException e) {
      e.printStackTrace();
    }  // end of try
  } // end of main
} // end of class SimpleServer

My question is why does it stuck in the loop ?


回答1:


You are reading the inpit until end of stream before you send anything. End of stream on a socket only occurs when the peer closes the connection. The peer hasn't closed the connection, because he wants to read your reply from it.

You should echo every line as you read it, not try to assemble them all and then echo them all in one great chunk. Apart from the fact that it can't work, your technique also wastes both time and space.



来源:https://stackoverflow.com/questions/40004783/printwriter-wont-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!