Php 尝试打印从更多客户端接收的邮件时出现问题

Php 尝试打印从更多客户端接收的邮件时出现问题,php,Php,让我解释一下问题:我正在创建一个系统,使用能够读取一些数据的传感器,然后,每个传感器都必须将这些数据发送到服务器,服务器将在页面上的新行中打印收到的每条消息 Sensor.php class Sensor extends Thread implements ISensor { .... public function readValue(){ $this->value = rand( -15 , 40 ); $this->timest

让我解释一下问题:我正在创建一个系统,使用能够读取一些数据的传感器,然后,每个传感器都必须将这些数据发送到服务器,服务器将在页面上的新行中打印收到的每条消息

Sensor.php

class Sensor extends Thread implements ISensor
{
    ....

    public function readValue(){
        $this->value = rand( -15 , 40 );
        $this->timestamp = date('Y-m-d H:i:s');
    }

    public function run(){
        $number = 0;
        $this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Couldn't create socket");
        $this->connectionResult = socket_connect($this->socket, $this->ip, (int)$this->port) or die("Couldn't connect to server");
        while($number <= $this->requestNumber){
            $number = $number + 1;
            $this->readValue();
            $this->sendData();
            sleep($this->frequency);
        }
        socket_close($this->socket);
    }

    public function sendData(){
        $input = $this->toString();
        socket_write($this->socket, $input, strlen($input)) or die ("Impossible send message");
    }
类传感器扩展线程实现ISensor
{
....
公共函数readValue(){
$this->value=rand(-15,40);
$this->timestamp=date('Y-m-dh:i:s');
}
公共功能运行(){
$number=0;
$this->socket=socket\u create(AF\u INET、SOCK\u STREAM、SOL\u TCP)或die(“无法创建套接字”);
$this->connectionResult=socket\u connect($this->socket,$this->ip,(int)$this->port)或die(“无法连接到服务器”);
while($number){
$number=$number+1;
$this->readValue();
$this->sendData();
睡眠($this->frequency);
}
插座\关闭($this->socket);
}
公共函数sendData(){
$input=$this->toString();
socket_write($this->socket,$input,strlen($input))或die(“不可能发送消息”);
}
Server.php

set_time_limit (300);
$address = '127.0.0.1';
$port = 19000;

$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_bind($sock, $address, $port) or die('Could not bind to address');
socket_listen($sock);

while (true) {
    $client = socket_accept($sock);
    while (0 != socket_recv($client, $message, 1024, 0))
    {
        echo $message . "<br>";
    }
    socket_close($client);
}
socket_close($sock);
设置时间限制(300);
$address='127.0.0.1';
$port=19000;
$sock=socket\u create(AF\u INET、sock\u STREAM、SOL\u TCP);
socket_bind($sock,$address,$port)或die('cannotbind to address');
socket_listen($sock);
while(true){
$client=socket\u accept($sock);
而(0!=socket_recv($client,$message,1024,0))
{
回显$message。“
”; } socket_close($client); } 插座关闭($sock);
预期的结果是,客户端能够连接到服务器并发送消息(这很好),而服务器只需为接收到的每条消息打印一行新行

这部分是有效的,我会用截图更好地解释(我实际上无法发布截图,如果你不能理解下面的文本描述的问题,我会留下一个指向截图图像的链接)

第一个传感器持续发送所有消息,服务器正确地打印它们,然后当线程结束时,服务器只输出第二个传感器在第一个传感器仍在运行时接收到的所有消息的整行,然后像它应该的那样打印消息


这是因为while循环?如果我删除while循环,服务器只需为每个传感器打印一条消息。

我实际上找到了解决方案

经过数小时的网络搜索,我终于找到了正确的键盘,上面显示了一个带有此代码的页面

ini_set('error_reporting', E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);

// Set time limit to indefinite execution
set_time_limit(0);

// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 15000;

//Implicit Flush
ob_implicit_flush();

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);

// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');

// Start listening for connections
socket_listen($sock);

// Non block socket type
socket_set_nonblock($sock);

// Clients
$clients = [];
// Loop continuously
while (true) {
    // Accept new connections
    if ($newsock = socket_accept($sock)) {
        if (is_resource($newsock)) {
            // Set Non-block for the new connection
            socket_set_nonblock($newsock);
            // Append the new connection to the clients array
            $clients[] = $newsock;
        }
    }
    // Polling for new messages
    if (count($clients)) {
        foreach ($clients AS $k => $v) {
            // Check for new messages
            $string = '';
            if ($char = socket_read($v, 1024)) {
                $string .= $char;
            }
            // New string for a connection
            if ($string) {
                echo "$string <br>";
            } else {
                if ($seconds > 30) {
                    // Ping every 5 seconds for live connections
                    if (false === socket_write($v, 'PING')) {
                        // Close non-responsive connection
                        socket_close($clients[$k]);
                        // Remove from active connections array
                        unset($clients[$k]);
                    }
                    // Reset counter
                    $seconds = 0;
                }
            }
        }
    }
    sleep(1);
    $seconds++;
}
// Close the master sockets
socket_close($sock);
ini\u集('error\u reporting',E\u ALL ^E\u NOTICE);
ini设置(“显示错误”,1);
//设定无限期执行的时间限制
设置时间限制(0);
//设置我们将侦听的ip和端口
$address='127.0.0.1';
$port=15000;
//隐式齐平
ob_implicit_flush();
//创建TCP流套接字
$sock=socket\u create(AF\u INET,sock\u STREAM,0);
//将套接字绑定到地址/端口
socket_bind($sock,$address,$port)或die('cannotbind to address');
//开始监听连接
socket_listen($sock);
//非块式插座
插座组非块($sock);
//客户
$clients=[];
//连续循环
while(true){
//接受新连接
如果($newsock=socket\u accept($sock)){
如果(是_资源($newsock)){
//为新连接设置非阻塞
插座组非块($newsock);
//将新连接附加到客户端阵列
$clients[]=$newstock;
}
}
//轮询新消息
如果(计入(客户)){
foreach($k=>v的客户机){
//检查是否有新消息
$string='';
如果($char=socket\u read($v,1024)){
$string.=$char;
}
//连接的新字符串
如果($string){
回显“$string
”; }否则{ 如果($s>30){ //每5秒Ping一次,用于实时连接 if(false==套接字写入($v,'PING')){ //关闭非响应连接 socket_close($clients[$k]); //从活动连接阵列中删除 未结算(客户[$k]); } //复位计数器 $seconds=0; } } } } 睡眠(1); $seconds++; } //关闭主插座 插座关闭($sock);
而且效果很好


感谢大家的帮助,我发布了自动安检器,以防其他人遇到类似问题。

我已经找到了解决方案

经过数小时的网络搜索,我终于找到了正确的键盘,上面显示了一个带有此代码的页面

ini_set('error_reporting', E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);

// Set time limit to indefinite execution
set_time_limit(0);

// Set the ip and port we will listen on
$address = '127.0.0.1';
$port = 15000;

//Implicit Flush
ob_implicit_flush();

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_STREAM, 0);

// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');

// Start listening for connections
socket_listen($sock);

// Non block socket type
socket_set_nonblock($sock);

// Clients
$clients = [];
// Loop continuously
while (true) {
    // Accept new connections
    if ($newsock = socket_accept($sock)) {
        if (is_resource($newsock)) {
            // Set Non-block for the new connection
            socket_set_nonblock($newsock);
            // Append the new connection to the clients array
            $clients[] = $newsock;
        }
    }
    // Polling for new messages
    if (count($clients)) {
        foreach ($clients AS $k => $v) {
            // Check for new messages
            $string = '';
            if ($char = socket_read($v, 1024)) {
                $string .= $char;
            }
            // New string for a connection
            if ($string) {
                echo "$string <br>";
            } else {
                if ($seconds > 30) {
                    // Ping every 5 seconds for live connections
                    if (false === socket_write($v, 'PING')) {
                        // Close non-responsive connection
                        socket_close($clients[$k]);
                        // Remove from active connections array
                        unset($clients[$k]);
                    }
                    // Reset counter
                    $seconds = 0;
                }
            }
        }
    }
    sleep(1);
    $seconds++;
}
// Close the master sockets
socket_close($sock);
ini\u集('error\u reporting',E\u ALL ^E\u NOTICE);
ini设置(“显示错误”,1);
//设定无限期执行的时间限制
设置时间限制(0);
//设置我们将侦听的ip和端口
$address='127.0.0.1';
$port=15000;
//隐式齐平
ob_implicit_flush();
//创建TCP流套接字
$sock=socket\u create(AF\u INET,sock\u STREAM,0);
//将套接字绑定到地址/端口
socket_bind($sock,$address,$port)或die('cannotbind to address');
//开始监听连接
socket_listen($sock);
//非块式插座
插座组非块($sock);
//客户
$clients=[];
//连续循环
while(true){
//接受新连接
如果($newsock=socket\u accept($sock)){
如果(是_资源($newsock)){
//为新连接设置非阻塞
插座组非块($newsock);
//将新连接附加到客户端阵列
$clients[]=$newstock;
}
}
//轮询新消息
如果(计入(客户)){
foreach($k=>v的客户机){
//检查是否有新消息
$string='';
如果($char=socket\u读取($