title: How to: Java Server Socket response and Client reading
body: <p>Imagine the next case:</p>
  
  <p>Client - server connection</p>
  
  <p>Client sends a request to the server
  Server answers the Client
  Client reads the answer</p>
  
  <p>Class Client:</p>
  
  <pre><code>public class Client extends Service{
  
      private  String IP_ADDRESS;
      private  int PORT;
  
      public void start(){
          l.info("Starting client for server at: "+IP_ADDRESS+":"+PORT);
          //Initialization of the client
          try {
              cs=new Socket(IP_ADDRESS,PORT);
          } catch (UnknownHostException e) {
              l.error("Unkown host at the specified address");
              e.printStackTrace();
          } catch (IOException e) {
  
  
          l.error("I/O error starting the client socket");
                  e.printStackTrace();
              }
          }
  
          //Sends the specified text by param
          public void sendText(String text){
              //Initializa the output client with the client socket data
              try {
                  //DataOutputStream to send data to the server
                  toServer=new DataOutputStream(cs.getOutputStream());
  
                  l.info("Sending message to the server");
  
                  PrintWriter writer= new PrintWriter(toServer);
                  writer.println(text);
                  writer.flush();
  
              } catch (IOException e) {
                  l.error("Bat initialization of the output client stream");
                  e.printStackTrace();
              }
          //Should show the answers from the server, i run this as a thread
          public void showServerOutput(){
              String message;
  
              while(true){
                  //If there are new messages
                  try {
                      BufferedReader br= new BufferedReader(new InputStreamReader((cs.getInputStream())));
                      if((message=br.readLine())!=null){
                          //Show them
                              System.out.println(message);
                      }
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          }
  
     }
  </code></pre>
  
  <p><code>showServerOutput()</code> is the method that returns any answer sent by the server</p>
  
  <p>Then my server class have the following code</p>
  
  <pre><code>public class Server extends Service{
      public void startListenner(){
          l.info("Listening at port "+PORT);
          while(true){
              // Waits for a client connection
              try {
                  cs=ss.accept();
                  l.info("Connection received: "+cs.getInetAddress()+":"+cs.getPort());
  
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
              try {
                  toClient= new DataOutputStream(cs.getOutputStream());
                  PrintWriter cWriter= new PrintWriter(toClient);
  
                  //Send a confirmation message
                  cWriter.println("Message received");
                  //Catch the information sent by the client
                  csInput=new BufferedReader(new InputStreamReader(cs.getInputStream()));
  
                  printData();
                  toClient.close();
  
              } catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
  
          }
      }
  }
  </code></pre>
  
  <p>As you can see im sending a message to the client with the words: "Message received" but its never shown in the client console. Whats wrong?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>The <code>printData()</code> method prints the message received from the client in console</p>
  
  <pre><code>public void printData(){
      l.info("Printing message received");
      try {
          System.out.println(csInput.readLine());
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  </code></pre>