PHP数组神秘行为为“未定义索引”

时间:2019-12-09 06:53:54

标签: php arrays sockets object

我正在使用PHP中的流方法练习套接字编程。 我正在使用PHP 7.3.11。 我发现了非常意外的错误消息,其中显示“未定义索引”错误

这是我的服务器端代码

 <?php
        error_reporting(E_ALL);
        /* allow the script to hang around waiting for connection.*/
        set_time_limit(0);

        $address = '127.0.0.1';
        $port = 9016;
        $server = stream_socket_server($address .":". $port, $errno, $errorMessage);    
        $req = array( "R004"=>"R004 exist"
    , "R001" => "R001 Ruccess"
    , "R002"=>"R002Rcontinue"
    , "R003"=>"R003 lRquit"
    , "R005"=>"R005 hello");
        $res = array("TT","SOO1", "S002", "S003");
        if ($server === false)
        {
            throw new UnexpectedValueException("Could not bind to socket: $errorMessage");
        }

        echo json_encode($req);

        while($socket = stream_socket_accept($server)){
            $rand = rand(1,3);
            $peer =  stream_socket_get_name($socket, true);
            $pkt = stream_socket_recvfrom($socket, 1500, 0,$peer);
            if( !empty($pkt)){
                $pkt = trim(preg_replace('/\s+/','',$pkt));
                echo "pkt[{$pkt}]";
                echo "EXITS:".array_key_exists($pkt, $req)."\n";
                echo "pkt[{$pkt}]".PHP_EOL;
                echo $req[$pkt].PHP_EOL;
                stream_socket_sendto($socket, $res[$rand], 0, $peer);
            }
            fclose($socket);
            usleep(1000);
        }

        stream_socket_shutdown($server, \STREAM_SHUT_RDWR);

这是我的客户端代码

error_reporting(E_ALL);
set_time_limit(0);

$address = '127.0.0.1';
$port = 9016;

$local = "tcp://{$address}:{$port}";

$socket = stream_socket_client($local, $errno, $errstr, 30);
$c_res = array("S001" => "Success", "S002" => "continue", "S003" => "quit");
$c_req = array("ROO3", "R005", "R002", "R001", "R004");

while ($socket = stream_socket_client($local, $errno, $errstr, 30)) {
    $rand = rand(1, 4);
//    echo "SEND[$c_req[$rand]]" . PHP_EOL;

    $sent = stream_socket_sendto($socket, $c_req[$rand] . PHP_EOL);

    if ($sent > 0) {
        usleep(1000);
        $s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
//        $s_res = fread($socket, 4096);
        $s_res = trim(preg_replace('/\s+/', '', $s_res));

        echo "c_res[{$s_res}]";
        echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
        echo "c_res[{$s_res}]" . PHP_EOL;
        echo $c_res[$s_res] . PHP_EOL;
    }
}

stream_socket_shutdown($socket, STREAM_SHUT_RDWR);

当我同时运行服务器和客户端代码时(服务器先运行,然后运行客户端)

我发现了这种错误消息

PHP Notice:  Undefined index: SOO1 in E:\xampp\htdocs\mave_gen\www\adm\client.php on line 29

我根本不理解此错误消息,因为除S001之外,所有其他参考均打印出OK。 显然,在代码中定义了S001。 我在这里想念什么吗?

先谢谢您。

1 个答案:

答案 0 :(得分:1)

在服务器端代码的$res = array("TT","SOO1", "S002", "S003");中,行SOO1应该为S001。您已在SOO1中输入字母“ O”,但根据其余代码,它应为“ 0”(零)。

在客户端代码中,修改以下代码行

if ($sent > 0) {
        usleep(1000);
        $s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
//        $s_res = fread($socket, 4096);
        $s_res = trim(preg_replace('/\s+/', '', $s_res));

        echo "c_res[{$s_res}]";
        echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
        echo "c_res[{$s_res}]" . PHP_EOL;
        echo $c_res[$s_res] . PHP_EOL;
    }

到下一行

if ($sent > 0) {
        usleep(1000);
        $s_res = stream_socket_recvfrom($socket, 1500, 0, $peer);
        if(!empty($s_res))
        {
//        $s_res = fread($socket, 4096);
        $s_res = trim(preg_replace('/\s+/', '', $s_res));


        echo "c_res[{$s_res}]";
        echo "EXITS:" . array_key_exists($s_res, $c_res) . "\n";
        echo "c_res[{$s_res}]" . PHP_EOL;
        echo $c_res[$s_res] . PHP_EOL;

     }
    }

这些更改后,它应该可以工作。