title: Java socket client not receiving data
body: <p>I seem to be having a few issues working with sockets in Java. The issue is that the client doesn't receive data and, when it does sometimes receive it, the data is delayed by a large amount of time. The server receives all data from the client correctly and responds how I would like it to (at least from the output it does). Any help on this would be helpful.</p>
  
  <p>Server:</p>
  
  <pre><code>try{
          ServerSocket server = new ServerSocket(6969);
          while (true){
              System.out.println("Listening on port 6969");
              Socket client = server.accept();
              System.out.println("Accepted");
              BufferedReader input = new BufferedReader(new InputStreamReader(client.getInputStream()));
              System.out.println("Got the input reader");
  
              //This is where the input data will be stored
              StringBuffer sb = new StringBuffer();
              while (input.ready()){
                  System.out.println("Reading data");
                  int cha = input.read();
                  if ((cha &lt; 0)){
                      break;
                  }
                  sb.append((char)cha);
              }
  
              System.out.println("Recived Data:" + sb.toString());
              BufferedWriter output = new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
              System.out.println(sb.toString().equalsIgnoreCase("INIT"));
              if (sb.toString().equalsIgnoreCase("INIT")){
                  JSONObject jo = new JSONObject();
                  jo.put("password", "Password123");
                  jo.put("testString", "tESTsTRING");
                  jo.put("intTest", 222);
                  System.out.println("Sending data:" + jo.toString());
                  output.write(jo.toString());
                  output.flush();
              }
  
          }
      }catch (Exception e){
          System.out.println(e.getMessage());
      }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>try{
          Socket connection = new Socket(host, 6969);
          System.out.println("Set up connection. Sending data and waiting for response");
          BufferedWriter output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
  
          output.write("INIT");
          output.flush();
  
          BufferedReader input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          System.out.println("Got the input reader: " + connection.getInputStream().available());
  
          //This is where the input data will be stored
          StringBuffer sb = new StringBuffer();
          System.out.println(input.readLine() + " :: " + input.ready());
          while (input.ready()){
              System.out.println("Reading data");
              int cha = input.read();
              if ((cha &lt; 0)){
                  break;
              }
              sb.append((char)cha);
          }
  
          System.out.println("Recived Data:" + sb.toString());
  
      }catch (Exception e){
          e.printStackTrace();
      }
  </code></pre>
  
  <p>The server console output:</p>
  
  <pre><code>Listening on port 6969
  Accepted
  Got the input reader
  Reading data
  Reading data
  Reading data
  Reading data
  Recived Data:INIT
  true
  Sending data:{"intTest":222,"testString":"tESTsTRING","password":"Password123"}
  </code></pre>
  
  <p>The client output:</p>
  
  <pre><code>Set up connection. Sending data and waiting for response
  Got the input reader: 0
  </code></pre>
  
  <p>Also, the server sometimes doesn't receive the data from the client even though it recognizes that the client is trying to send the data.</p>