title: Connection Reset By Peer
body: <p>It's about a client (PHP) server (Java) environment. I get the following exception on the Java side (by out.flush())when the php requests for a web page that is about 40k characters long. I have used this architecture many, many times without getting any exception. As result, a page is shown, but not entirely.</p>
  
  <pre><code>java.net.SocketException: Connection reset by peer: socket write error
  at java.net.SocketOutputStream.socketWrite0(Native Method)
  at java.net.SocketOutputStream.socketWrite(Unknown Source)
  at java.net.SocketOutputStream.write(Unknown Source)
  at sun.nio.cs.StreamEncoder.writeBytes(Unknown Source)
  at sun.nio.cs.StreamEncoder.implWrite(Unknown Source)
  at sun.nio.cs.StreamEncoder.write(Unknown Source)
  at java.io.OutputStreamWriter.write(Unknown Source)
  at java.io.BufferedWriter.flushBuffer(Unknown Source)
  at java.io.BufferedWriter.flush(Unknown Source)
  at sitodove.core.net.Connection.writeMessage(Connection.java:79)
  at sitodove.core.net.FrontendConnection.run(FrontendConnection.java:149)
  </code></pre>
  
  <p>Here is the Java code that sends a certain string as a response to every PHP request</p>
  
  <pre><code>    public void writeMessage(String message) throws IOException
  {
      Logger.log("writeMessage, Valore in entrata: "+message);
      int messageLength=message.length();
      String startingNumber=""+messageLength;
      StringBuffer prefix=new StringBuffer(""+messageLength);
      for(int i=0;i&lt;PROTOCOL_PREFIX_LENGTH-startingNumber.length();i++)
          prefix.insert(0, "0");
      prefix.append(message);
      Logger.log("writeMessage, Valore in uscita: "+prefix);
      out.write(prefix.toString());
      out.flush();
      Logger.log("DATI INVIATI");
  }
  </code></pre>
  
  <p>Here is the PHP code that makes the request and reads the response from the Java server</p>
  
  <pre><code>$SEPARATOR='!!@!!';
  function communicate($remoteServer, $remotePort, $outText) 
  {
      $outTextLength = strlen($outText);
      $lengthPaddedPrefix = str_pad($outTextLength, 8, "0", STR_PAD_LEFT);
      $message = $lengthPaddedPrefix.$outText;
  
      $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socket");
      socket_connect($socket, $remoteServer, $remotePort) or  header( 'Location: /maintenance/' );
  
      socket_write($socket, $message, strlen($message)) or  header( 'Location: /maintenance/' );
      //echo 'COMUNICATO';
      $response = socket_read($socket, 20000000) or  header( 'Location: /maintenance/' );
  
      socket_close($socket);
  
      if (strpos($response, "JAVA is not responding") !== false) {
              die("Site is down for maintenance");
      }
      return substr($response, 8);
  }
  
  $coreanswer=$coreanswer=communicate('localhost', 6527, "showreport".$SEPARATOR.$_GET['uid'].$SEPARATOR.$_GET['wid'].$SEPARATOR.$_GET['getime'].$SEPARATOR.$_GET['rct']);
  echo $coreanswer;
  </code></pre>