<?php
namespace Swoole;
class TcpServer
{
// 系统支持的最大子进程数
const MAX_PROCESS = 3;
// 子进程pid数组
private $pids = [];
// 网络套接字
private $socket;
// 主进程 ID
private $mpid;
/**
* 服务器主进程业务逻辑
*/
public function run()
{
// 主进程
$process = new Process(function () {
// 获取当前进程 ID 作为主进程 ID
$this->mpid = posix_getpid();
echo time() . " Master process, pid {$this->mpid}\n";
// 创建 TCP 服务器并获取套接字
$this->socket = stream_socket_server("tcp://127.0.0.1:9503", $errno, $errstr);
if (!$this->socket) {
exit("Server start error: $errstr --- $errno");
}
// 启动子进程处理请求
for ($i = 0; $i < self::MAX_PROCESS; $i++) {
$this->startWorkerProcess();
}
echo "Waiting client start...\n";
/
简单的多进程 TCP 服务器实现
最新推荐文章于 2023-02-23 02:07:08 发布