title: Java Socket why server can not reply client
body: <p>I wanna write the code to let Client send a string to Server, Server print the string and reply a string, then Client print the string Server reply.<br>
  My Server</p>
  
  <pre><code>public class Server {
  
  public static void main(String[] args) throws IOException {
      ServerSocket ss = null;
      Socket s = null;
      try {
          ss = new ServerSocket(34000);
          s = ss.accept();
          BufferedReader in = new BufferedReader(new InputStreamReader(
                  s.getInputStream()));
          OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream());
  
          while (true) {
              String string = in.readLine();
              if (string != null) {
                  System.out.println("br: " + string);
  
                  if (string.equals("end")) {
                      out.write("to end");
                      out.flush();
                      out.close();
                      System.out.println("end");
                      // break;
                  }
              }
          }
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          s.close();
          ss.close();
      }
  }
  }
  </code></pre>
  
  <p>My Client:</p>
  
  <pre><code>public class Client {
  public static void main(String[] args) {
      Socket socket =null;
  
  
      try {
          socket = new Socket("localhost", 34000);
          BufferedReader in =new BufferedReader(new InputStreamReader(socket.getInputStream()));
          OutputStreamWriter out = new OutputStreamWriter(socket.getOutputStream());
  
          String string = "";
          string = "end";
          out.write(string);
          out.flush();
          while(true){
              String string2 = in.readLine();
              if(string2.equals("to end")){
                  System.out.println("yes sir");
                  break;
              }
          }
  
  
      }  catch (Exception e) {
          e.printStackTrace();
      }finally{
          try {
              System.out.println("closed client");
              socket.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
  }
  }
  </code></pre>
  
  <p>are there some somethings wrong? if i remove the code "while(true) ..." in client class, it's OK.</p>