如何使用Terminal/PHP测试PHP套接字TCP/IP服务器?
我有一个在线服务器,我用PHP制作了一个简单的TCP/IP服务器来处理某个端口。我使用的代码如下所示:如何使用Terminal/PHP测试PHP套接字TCP/IP服务器?,php,sockets,tcp,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
<?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
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();
我将尝试用于您的测试目的。它允许快速创建客户端和服务器。请尝试以下客户端代码以开始:
<?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();
如何安装ReactPHP?@Razgriz use:composer require react/socket
如何安装ReactPHP?@Razgriz use:composer require react/socket