PHP socket_recv换行问题

时间:2014-05-21 15:56:12

标签: php sockets newline php-5.4

我有以下内容从缓冲区读取并向套接字发送消息。

根据客户端(经典,telnet,windows,linux,putty等),它的行为有所不同。 它会在换行符之后发送行,有时则按字符发送。

我需要添加一个控件,以便它只在检测到换行符后发送消息。

有什么想法吗?

foreach ($this->changed as $key => $socket) {
    $buffer = null;
    while (socket_recv($socket, $buffer, 1024, 0) >= 1) {
        $this->sendMessage(trim($buffer) . PHP_EOL);
        unset($this->changed[$key]);
        break;
    }
}

这是sendMessage函数

function sendMessage($msg)
{
    foreach($this->clients as $client)
    {
        @socket_write($client,$msg,strlen($msg));
    }
    return true;
}

1 个答案:

答案 0 :(得分:0)

foreach ($this->changed as $key => $socket) 
{
    $buffer = null;
    $tempbuffer=null;
    while(socket_recv($socket, $tempbuffer, 1024, 0) >= 1) {
        if(strpos($tempbuffer,'\n')==false)
            $tempbuffer.=$buffer;
    }
    if($buffer==null)
       $buffer=$tempbuffer;

    $this->sendMessage(trim($buffer) . PHP_EOL);
    unset($this->changed[$key]);

    break;

}