title: Connect to listening netcat using PHP
body: <p>I have a netcat server, running from command line:</p>
  
  <pre><code>netcat -l -q0 -p 9999 -c "tesseract stdin stdout -l spa"
  </code></pre>
  
  <p>I can connect to this server using netcat client:</p>
  
  <pre><code>cat /tmp/autogenerado.jpg | netcat 127.0.0.1 9999
  </code></pre>
  
  <p>So, I send a jpeg file to another server, and receive the OCR'ed data in client.</p>
  
  <p>I want to do this in PHP:</p>
  
  <pre><code>$fp = fsockopen("tcp://127.0.0.1:9999", $errno, $errstr); 
  
  if (!$fp) {
      echo "{$errno}: {$errstr}\n";
      die();
  }
  
  fwrite($fp, $jpeg);
  
  $contents = stream_get_contents($fp);
  
  fclose($fp);
  
  return $contents;
  </code></pre>
  
  <p>But the script hangs on <code>stream_get_contents</code>. I think it is because the netcat server doesn't know when the PHP client has finished sending data. </p>
  
  <p>Is it possible to send an EOF ?</p>