title: PHP silently fails sending data to graphite over UDP using fsockopen and fwrite
body: <h2>The code</h2>
  
  <p>I'm sending metrics to <a href="https://github.com/graphite-project/graphite-web" rel="nofollow" title="graphite">graphite</a> via UDP using both php and python.</p>
  
  <p>My <strong>python</strong> client looks like this</p>
  
  <pre><code>#!/usr/bin/python
  import time
  from socket import socket
  
  sock = socket()
  try:
    sock.connect( ('127.0.0.1', 2003) )
  except:
    print 'network error'
    sys.exit(12)
  
  message = ("some.custom.metric.python 1 %d\n" % (int( time.time() )))
  print message
  sock.sendall(message)
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>some.custom.metric.python 1 1376045467</p>
  </blockquote>
  
  <p>And my <strong>php</strong> client like this</p>
  
  <pre><code>&lt;?php
  
  try {
      $fp = fsockopen("udp://127.0.0.1", 2003, $errno, $errstr);
  
      if (!empty($errno)) echo $errno;
      if (!empty($errstr)) echo $errstr;
  
      $message = "some.custom.metric.php 1 ".time().PHP_EOL;
      $bytes = fwrite($fp, $message);
      echo $message;
  } catch (Exception $e) {
      echo "\nNetwork error: ".$e-&gt;getMessage();
  }
  </code></pre>
  
  <p>Output:</p>
  
  <blockquote>
    <p>some.custom.metric.php 1 1376042961 </p>
  </blockquote>
  
  <h2>Testing</h2>
  
  <p>I start <strong>carbon</strong> enabling debug output:</p>
  
  <pre><code>/opt/graphite/bin/carbon-cache.py --debug start
  </code></pre>
  
  <p>When I run my <strong>python</strong> client it works just fine, and I can see it on the debug output</p>
  
  <pre><code>09/08/2013 13:13:05 :: [listener] MetricLineReceiver connection with 127.0.0.1:58134 established
  09/08/2013 13:13:05 :: [listener] MetricLineReceiver connection with 127.0.0.1:58134 closed cleanly
  </code></pre>
  
  <p>I do the same via CLI using <strong>netcat</strong></p>
  
  <pre><code>echo "some.custom.metric.netcat 1 `date +%s`" | nc -w 1 127.0.0.1 2003
  </code></pre>
  
  <p>And I can see the connection in the debug output</p>
  
  <blockquote>
  <pre><code>09/08/2013 13:17:46 :: [listener] MetricLineReceiver connection with 127.0.0.1:58136 established
  09/08/2013 13:17:48 :: [listener] MetricLineReceiver connection with 127.0.0.1:58136 closed cleanly
  </code></pre>
  </blockquote>
  
  <h2>The problem</h2>
  
  <p>My php client is never communicating with carbon. Even if I use a different port where there's no app listening my PHP just tells me everything's fine. if I do the same on my python client I get a network error.</p>
  
  <p>According to the PHP docs, fsockopen <strong>never</strong> fails when using UDP because of the nature of the protocol, but I should get an error when executing the <strong>fwrite</strong>. In my case the fwrite always returns the len() of the $message no matter which host/port I use when opening the socket.</p>
  
  <p>If I use a wrong port with netcat or the python client i get a network error as expected.</p>
  
  <p>PHP-cli has <code>error_display = On</code> and <code>error_reporting = E_ALL</code>. I've tested this on PHP 5.4.4-14 on debian 7.1 and PHP 5.5 on Windows 7.</p>
  
  <p>Has anybody run into something similar to this? I'm almost sure there's no problem with my graphite or network configurations, so I bet it has something to do with PHP.</p>