如何在流套接字中检测中止的连接-php

如何在流套接字中检测中止的连接-php,php,sockets,Php,Sockets,Im使用此代码接收数据并将数据发送回对等方: $sock = stream_socket_server("tcp://127.0.0.1:9000", $errno, $errorMessage); if (!$sock) { echo "error code: $errno \n error msg: $errorMessage"; } $read[0] = $sock; $write = null; $except = null; $ready = stream_select($re

Im使用此代码接收数据并将数据发送回对等方:

$sock = stream_socket_server("tcp://127.0.0.1:9000", $errno, $errorMessage);
if (!$sock) {
    echo "error code: $errno \n error msg: $errorMessage";
}
$read[0] = $sock;
$write = null;
$except = null;
$ready = stream_select($read,$write,$except,10);
if ($ready) {
    $a = @stream_socket_accept($sock);

    $in = '';
    do {
        $temp = fread($a,1024);
        $in .= $temp;
    } while (strlen($temp));
    var_dump($in);

    $out = '....'//some data
    $out2 = '....'//some data
    fwrite($a,$out);
    fwrite($a,$out2);
}       
但是第二次写给我这个错误:

注意:fwrite():发送6字节失败,错误号为10053 已建立的连接已被主机中的软件中止 机器


现在,如何在发送数据之前检测中断的连接?

我有类似的方法,我的解决方案是将PHP警告转换为异常,并以这种方式进行处理。具体而言:

set_error_handler("warning_handler", E_WARNING);    
try{
    $res = fwrite($a,$out);
} catch(Exception $e){
    //handle the exception, you can use $e->getCode(), $e->getMessage()
}   
restore_error_handler();    
....
function warning_handler($errno, $errstr) { 
    throw new Exception($errstr, $errno);   
}
恢复错误处理程序似乎是最好的方法,这样就不会在其他地方弄乱代码