title: How can i get the reply from server TCP in java
body: <p>My code just do a simple task send a text from client's console to server and receive a reply. But my code doesn't work though. I keep sending text to server and no reply sending back. I have done a several example that plus 2 number given from client. I do this the same way but i can't figure out what is the problem.</p>
  
  <p>Server:</p>
  
  <pre><code>public class Server {
      public static void main(String[] args) {
          try {
              ServerSocket server = new ServerSocket(8);
              Socket client = server.accept();
              BufferedReader inFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
              BufferedWriter outToClient = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
              String in = inFromClient.readLine(),out;
              while(in!=null){
                  out = in+" from server";
                  outToClient.write(out);
                  outToClient.newLine();
                  outToClient.flush();
              }
              inFromClient.close();
              outToClient.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>public class Client {
      public static void main(String[] args) {
          try {
              Socket client = new Socket("localhost", 8);
              System.out.println("Connected to server");
              BufferedReader inFromServer = new BufferedReader(new InputStreamReader(client.getInputStream()));
              BufferedWriter outToServer = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
              Scanner input = new Scanner(System.in);
              String strClient,strServer;
              while(true){
                  System.out.print("Client: ");
                  strClient = input.nextLine();
                  outToServer.write(strClient);
                  strServer = inFromServer.readLine();
                  System.out.print("Server: ");
                  System.out.println(strServer);
              }
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>