I donβt think the stream_socket family is stream_socket , it seems like this is too high a level.
I tried to make a very hacky decision, I do not know if it will work for you, it is not very reliable:
<?php set_error_handler('my_error_handler'); function my_error_handler($no,$str,$file,$line) { throw new ErrorException($str,$no,0,$file,$line); } $socket = stream_socket_server("tcp://0.0.0.0:8000", $errno, $errstr); if (!$socket) { echo "$errstr ($errno)\n"; } else { while ($conn = stream_socket_accept($socket)) { foreach (str_split('The local time is ' . date('n/j/Y g:i a') . "\n") as $char) { echo $char; try { fwrite($conn,$char); } catch (ErrorException $e) { if (preg_match("/^fwrite\(\): send of 1 bytes failed with errno=([0-9]+) ([A-Za-z \/]+)$/",$e->getMessage(), $matches)) { list($errno,$errstr) = array((int) $matches[1], $matches[2]); if ($errno === 32) { echo "\n[ERROR] $errstr";
Launch: php ./server.php
Connect: nc localhost 8000 | head -c1 nc localhost 8000 | head -c1
Server output:
The loca [ERROR] Broken pipe [ERROR] Couldn't write more on Resource id
Janus Troelsen
source share