windows 搭建 swoole开发环境(官网已支持)

第一步下载:swoole官网下载 swoole-cli-v5.0.3-cygwin-x64.zip 只支持 64 位的系统
第二步解压到指定文件夹:E:\phpstudy_pro\WWW\swoole-cli-v5.0.3-cygwin-x64
第三步设置环境变量:把解压后的文件夹下的 bin 目录路径配置到系统的 Path 环境变量中,确定保存
第四步检查安装情况:打开CMD命令行输入:swoole-cli -v,安装成功
第五步:编写简单的TCP服务器代码:TCP.php

1. 服务端:

php 复制代码
<?php

class TCP
{

    private $server = null;

    public function __construct()
    {
        $this->server = new Swoole\Server('127.0.0.1', 9501);

        $this->server->set(array(
            'worker_num' => 4,     // 进程数
            'max_request' => 50,    // 每个进程最大接受请求数
        ));

        //监听连接进入事件。
        $this->server->on('Connect', [$this, 'onConnect']);

        //监听数据接收事件。
        $this->server->on('Receive', [$this, 'onReceive']);

        监听连接关闭事件。
        $this->server->on('Close', [$this, 'onClose']);

        //启动服务器
        $this->server->start();
    }

    public function onConnect($server, $fd)
    {
        echo "客户端id: {$fd}连接.\n";
    }

    public function onReceive($server, $fd, $reactor_id, $data)
    {
        $server->send($fd, "发送的数据: {$data}");
    }

    public function onClose($server, $fd)
    {
        echo "客户端id: {$fd}关闭.\n";
    }

}


new TCP();

运行:

2. 客户端:

php 复制代码
<?php

use Swoole\Coroutine\Client;
use function Swoole\Coroutine\run;

run(function () {
    $client = new Client(SWOOLE_SOCK_TCP);
    if (!$client->connect('127.0.0.1', 9501, 0.5)) {
        echo "connect failed. Error: {$client->errCode}\n";
    }

    fwrite(STDOUT, '请输入');
    $res = fgets(STDIN);
    $client->send($res);
    echo $client->recv();
    $client->close();
});

运行:

相关推荐
Sherotree3 小时前
Google Antigravity——Agent 被提到主界面
前端·ide·后端·edge浏览器
DogDaoDao3 小时前
Windows 开发提效工具全景指南:60+ 工具的工程化分层配置
windows·git·程序员·开发工具·powershell·everything·msys2
2601_962056233 小时前
Spring Boot 入门 与 无法解析符号 springframework 的解决
java·spring boot·后端
你驴我4 小时前
WhatsApp 多账号场景下的会话归档与历史消息检索优化实践
后端·python
学长毕业设计4 小时前
基于SpringBoot的瑜伽馆网站的设计与实现(源码+文档+讲解视频)
java·spring boot·后端
Rain的Java大神之路4 小时前
PC版网站被狂刷怎么处理
java·数据库·redis·后端·mysql·web安全·运维开发
学长毕业设计7 小时前
基于springboot的云南中草药知识普及管理网站的设计与开发(源码+文档+讲解视频)
java·spring boot·后端
烂蜻蜓8 小时前
Flask入门教程(八):视图函数详解——请求处理与响应的核心
后端·python·flask
水饺编程8 小时前
第5章,[Win32 章节] :Polygon 函数和多边形填充模式
c语言·c++·windows·visual studio