尝试打印从更多客户端收到的消息时出现问题

时间:2019-04-27 13:11:13

标签: php

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

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");
    }

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);

预期结果是客户端能够连接到服务器并发送消息(这很好),而服务器只是为收到的每条消息打印新行。

部分有效,我将用屏幕截图更好地解释(我实际上无法发布屏幕截图,如果您无法理解下面的文字说明问题,我会留下指向屏幕截图图像的链接)

enter image description here

第一个传感器继续发送所有消息,服务器正确地打印它们,然后,当线程结束时,服务器仅输出从第二个第二个传感器接收的所有消息的整行,而第一个传感器仍在运行,然后打印像这样的消息。

是因为while循环?如果删除了while循环,则服务器仅为每个传感器打印一条消息。

1 个答案:

答案 0 :(得分:0)

我实际上找到了解决方法。

经过数小时的网络搜索,我终于找到了正确的密码,向我显示了包含此代码的页面

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);

效果很好!

感谢大家的帮助,我会发布自动答案,以防其他人遇到类似问题。

相关问题