PHP silently fails sending data to graphite over UDP using fsockopen and fwrite
Question: ## The code

I'm sending metrics to [graphite](https://github.com/graphite-
project/graphite-web "graphite") via UDP using both php and python.

My **python** client looks like this

    
    
    #!/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)
    

Output:

> some.custom.metric.python 1 1376045467

And my **php** client like this

    
    
    <?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->getMessage();
    }
    

Output:

> some.custom.metric.php 1 1376042961

## Testing

I start **carbon** enabling debug output:

    
    
    /opt/graphite/bin/carbon-cache.py --debug start
    

When I run my **python** client it works just fine, and I can see it on the
debug output

    
    
    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
    

I do the same via CLI using **netcat**

    
    
    echo "some.custom.metric.netcat 1 `date +%s`" | nc -w 1 127.0.0.1 2003
    

And I can see the connection in the debug output

>
>     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
>  

## The problem

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.

According to the PHP docs, fsockopen **never** fails when using UDP because of
the nature of the protocol, but I should get an error when executing the
**fwrite**. In my case the fwrite always returns the len() of the $message no
matter which host/port I use when opening the socket.

If I use a wrong port with netcat or the python client i get a network error
as expected.

PHP-cli has `error_display = On` and `error_reporting = E_ALL`. I've tested
this on PHP 5.4.4-14 on debian 7.1 and PHP 5.5 on Windows 7.

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.


Answer: Check if you have an open UDP port (it's off by default). The code (simplified
version) i used for testing (uses TCP)

    
    
    $line = "foo.bla 1 " . time() . "\n";
    
    $fp = fsockopen('127.0.0.1', 2003, $err, $errc, 1);
    fwrite($fp, $line);
    
    fclose($fp);
    

