如何知道什么时候连接被php中的对等重置?

use*_*594 8 php timeout tcp connection-close

我最近一直在使用PHP构建TCP服务器(我知道错误的选择,但这是工作标准),所以我已经达到了一个可靠的原型来对它进行测试并且它显示了良好的结果.在开始我使用套接字函数来处理服务器的连接,它工作正常,但项目的主要内容之一是使通道安全,所以我切换到stream_socket.

我想要的是stream_socket组中的socket_last_error等价物,所以我可以知道什么时候与客户端的连接关闭.当前情况所有进程都会等待超时计时器释放,即使客户端已经关闭.

我在网上搜索过,我发现没有办法通过PHP弄明白,我发现有些人打开了关于它的问题票,要求socket_last_error等效于流. https://bugs.php.net/bug.php?id=34380

那么无论如何都知道FIN_WAIT信号何时被提升?

谢谢,

Jan*_*sen 1

我觉得这家人不可能stream_socket,看起来层次太高了。

我尝试制作一个非常hackish的解决方案,我不知道它是否适合你,它不是很可靠:

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

发射:php ./server.php

连接:nc localhost 8000 | head -c1

服务器输出:

The loca
[ERROR] Broken pipe
[ERROR] Couldn't write more on Resource id #6
Run Code Online (Sandbox Code Playgroud)