title: Socket data does not appear to be getting through to client
body: <p>I've written some serverside socket handling code and I'm concerned that potentially my packets are not always making it back to the client.  I am logging all my events and in my log files it says I am sending the information.  But the client is also logging events and in their logs they say they do not receive anything.</p>
  
  <p>My code to send the data is as follows:</p>
  
  <pre><code>public void write(Packet packet) {
      String data = packet.serialize();
      log("Send=[" + data + "]", "Write"); // log to file
  
      try {
          _writer.write(data);
          _writer.flush();
      } catch (Exception ex) {
          log(ex, "write");
      }
  }
  </code></pre>
  
  <p>Each socket is created on a new thread and I create my writers and readers immediately like so (in the public run method):</p>
  
  <pre><code>// _sockt is a Java Socket object
  _writer = new BufferedWriter(new OutputStreamWriter(_socket
                      .getOutputStream()));
              _reader = new SocketReader(_socket);
  </code></pre>
  
  <p>SocketReader is just a wrapper class I created for listening for responses and has a public read method like so:</p>
  
  <pre><code>public String read() throws IOException, SocketTimeoutException {
  
      _socket.setSoTimeout(_timeOut);
  
      if(_reader == null)
          _reader = new BufferedReader(new InputStreamReader(_socket.getInputStream()));
  
              // read from the stream
      return new PacketDataInputStream(_reader).read();
  }
  </code></pre>
  
  <p>The PacketDataInputStream wrapper class:</p>
  
  <pre><code>BufferedReader _reader = null;
  
  public PacketDataInputStream(BufferedReader reader)
  {
      _reader = reader;
  }
  
  public String read() throws IOException, SocketException {
  
      StringBuilder builder = new StringBuilder();
      int c = 0;
  
      while((c = _reader.read()) != -1)
      {
          char ch = (char)c;
  
          builder.append(ch);
  
          if(ch == PacketConstants.ETX)
              break;
      }
  
      if(builder.length() &gt; 0)
          return builder.toString();
      else
          return null;
  
  }
  </code></pre>
  
  <p>The way I'm creating the actual socket listener objects is pretty standard I think:</p>
  
  <pre><code>InetAddress address = InetAddress.getByName(IP);
          server = new ServerSocket( port, 0, address);
  
                      // My own manager class to handle all the sockets connected 
          WebSocketManager manager = new WebSocketManager(this);
  
          Socket connection = null;
  
          while(bContinue)
          {
              connection = server.accept();
  
              if(bContinue) {
                                      // assign the socket to a new thread and start
                                      // that thread
                  manager.newSocket(connection);  
              } else {
                  connection.close();
              }
          }
  </code></pre>
  
  <ul>
  <li>Is is possible that I'm using the wrong objects for sending the data back.  </li>
  <li>Should I even be using a bufferedwriter and reader?  I had thought that these were the best way to go but now I'm not so sure. </li>
  </ul>
  
  <p>It's important to note that this does not happen all the time, just sporadically.  It could be the clients code having bugs but I need to make sure that I'm doing it correctly before going back to them.</p>
  
  <p>This code is run on a <em>Linux Ubuntu</em> server.  Logging occurs to a text file, nothing special there.  My log files show the <strong>Send=""</strong> data going back to the client and no exception so it appears as if the .write and .flush() worked?  Socket connections are persistant and only closed by the client and or network issues.</p>
  
  <p><strong>UPDATE ----- Client Side code -------:</strong></p>
  
  <p>I did manage to get some of the client side code for how they are handling the send and receiving of data (just in case it's more obvious on their end).  The client is actually connecting to this server via an <strong>Android device</strong> (if that helps).</p>
  
  <p>Creation of socket</p>
  
  <pre><code>static final int BUFFER_SIZE = 20000;       // Maximum packet size
  
  java.net.InetAddress server = java.net.InetAddress.getByName(url);
  socket = new Socket(server, port);
  // Set socket options:
  socket.setReceiveBufferSize(BUFFER_SIZE);
  socket.setSendBufferSize(BUFFER_SIZE);
  socket.setKeepAlive(true);
  socket.setTcpNoDelay(true);
  </code></pre>
  
  <p>Sending:</p>
  
  <pre><code>try {
              // Send the packet:
          OutputStream stream = socket.getOutputStream();
          stream.write(p.getByteArray ());
          stream.flush();
  
              // Update the time:
          lastPacketSendTime = new Date ();
      } catch (IOException e) {
          setError("Error sending packet (" + e.getMessage() + ")", ERROR_IO);
          return false;
      }
  </code></pre>
  
  <p>Receiving:</p>
  
  <pre><code>socket.setSoTimeout(timeout);
              // Get the reader:
          inputStream = socket.getInputStream();
  
  
  
       while (true) {
                      // Get the next character:
                  int value = inputStream.read();
                      // Check for -1, indicating that the socket is closed:
                  if (value == -1) {
                          // The socket is closed remotely, so close it locally as well:
                      disconnect();
                      inputStream = null;
                      return null;
                  }
                                  // ... and a bunch of other stuff to handle the actual data
  
                          }
  </code></pre>
  
  <p><strong>EDIT 14-Nov:</strong>
  This is actually proving to be more of a problem now.  Both the client logs and the server logs appear to be sending.  But at times the data doesn't appear to come through or if it does it is sometimes coming through 10 - 30 - 60 second delayed.</p>
  
  <p>I can provide more information if required.</p>