如何使用Terminal / PHP测试PHP套接字TCP / IP服务器?

时间:2017-11-16 13:00:29

标签: php sockets tcp

我有一台在线服务器,我使用PHP制作了一个简单的TCP / IP服务器来处理某个端口。我使用的代码如下:

<?php
error_reporting(E_ALL);

/* Allow the script to wait for connections. */
set_time_limit(0);

/* Activate the implicit exit dump, so we'll see what we're getting
* while messages come. */
ob_implicit_flush();

$address = '123.456.789.123';
$port = 1234;

if (($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}

if (socket_bind($sock, $address, $port) === false) {
    echo "socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

if (socket_listen($sock, 5) === false) {
    echo "socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
}

//clients array
$clients = array();

do {
    $read = array();
    $read[] = $sock;

    $read = array_merge($read,$clients);

    $write = NULL;
    $except = NULL;
    $tv_sec = 5;

    // Set up a blocking call to socket_select
    if(socket_select($read, $write, $except, $tv_sec) < 1)
    {
        //    SocketServer::debug("Problem blocking socket_select?");
        echo "socket continuing";
        continue;
    }

    // Handle new Connections
    if (in_array($sock, $read)) {        

        if (($msgsock = socket_accept($sock)) === false) {
            echo "socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n";
            break;
        }
        $clients[] = $msgsock;
        $key = array_keys($clients, $msgsock);

        $msg = "\Welcome to the PHP Test Server. \n" .
        "You are the customer number: {$key[0]}\n" .
        "To exit, type 'quit'. To close the server type 'shutdown'.\n";
        socket_write($msgsock, $msg, strlen($msg));

    }

    // Handle Input
    foreach ($clients as $key => $client) { // for each client        
        if (in_array($client, $read)) {
            if (false === ($buf = socket_read($client, 2048, PHP_NORMAL_READ))) {
                echo "socket_read() failed: reason: " . socket_strerror(socket_last_error($client)) . "\n";
                break 2;
            }
            if (!$buf = trim($buf)) {
                continue;
            }
            if ($buf == 'quit') {
                unset($clients[$key]);
                socket_close($client);
                break;
            }
            if ($buf == 'shutdown') {
                socket_close($client);
                break 2;
            }
            $talkback = "Client {$key}: You said '$buf'.\n";
            socket_write($client, $talkback, strlen($talkback));
            echo "$buf\n";
        }

    }        
} while (true);

socket_close($sock);
?>

脚本基本上运行并允许运行多个连接。将代码上传到服务器后,我连接到服务器,到达文件所在的目录,然后运行php filename.php。它没有显示任何警告或错误。

但是,我需要进一步配置此TCP / IP服务器并根据收到的输入执行操作。现在,当我运行php filename.php时,它没有显示任何内容(我猜测是因为所有输出都写入套接字,而不是回显)。

如何测试我制作的TCP / IP服务器? telnet是不可能的,因为它不安全。现在我找不到太复杂的东西,所以终端或其他简单的PHP文件将是一个不错的选择。

1 个答案:

答案 0 :(得分:0)

我会尝试使用react/socket进行测试。它允许非常快速地创建客户端和服务器。尝试使用此客户端代码即可开始使用:

<?php

require_once __DIR__ . "/vendor/autoload.php";

$loop = React\EventLoop\Factory::create();
$connector = new React\Socket\Connector($loop);

$connector->connect('127.0.0.1:1234')->then(function (React\Socket\ConnectionInterface $conn) use ($loop) {

    $conn->on('data', function ($data) use ($conn) {

        echo $data;

        $conn->close();
    });
});

$loop->run();

文档为here