How to know whenever the connection is reset by peer in php? How to know whenever the connection is reset by peer in php? php php

How to know whenever the connection is reset by peer in php?


I don't think it's possible the stream_socket family, it looks like it's too high level.

I tried making a very hackish solution, I don't know if it will work for you, it's not very reliable:

<?phpset_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"; // Broken pipe                    }            }            echo "\n[ERROR] Couldn't write more on $conn";            break;      }      fflush($conn);    }    fclose($conn);  }  fclose($socket);}echo "\n";?>

Launch: php ./server.php

Connect: nc localhost 8000 | head -c1

Server output:

The loca[ERROR] Broken pipe[ERROR] Couldn't write more on Resource id #6