Home PHP Framework Swoole Record Swoole study notes

Record Swoole study notes

Jan 22, 2021 am 10:07 AM

Record Swoole study notes

Recommended (free): swoole

1. Installation

Currently there are two officially recommended methods

1). Use pecl to install

pecl install swoole
Copy after login

2). Use source code to install, it is recommended to download releases version of swoole, it is best not to pull the code and compile it from the github trunk, but download the tar package directly.

swoole package download address

Then compile and install

    wget https://github.com/swoole/swoole-src/archive/v2.0.7.tar.gz
    tar -zxf v2.0.7.tar.gz
    cd swoole-src-2.0.7/
    phpize     //如果执行这个命令没有任何显示的话,使用apt-get install php7.0-dev安装包
    ./configure
    make && make install
Copy after login

2. Change the php.ini extension

Modify php.ini configuration file, use the command php -i |grep php.ini to view the php.ini location
Add configuration

    extension=swoole.so
Copy after login

Use php -m or phpinfo() to check whether swoole is loaded successfully

3. Chestnut TCP server, three-way handshake

Simple understanding of Socket

Write server.php

    //创建Server对象,监听 127.0.0.1:9501端口$serv = new swoole_server("127.0.0.1", 9501); 

    //监听连接进入事件$serv->on('connect', function ($serv, $fd) { 
        echo "Client: Connect.\n";
    });

    //监听数据接收事件$serv->on('receive', function ($serv, $fd, $from_id, $data) {
        $serv->send($fd, "Server: ".$data);
        echo "Receive message:$data";
        //关闭连接(当然,也可以不关闭,不关闭的话会一直等待接收命令而无法退出)
        $serv->close($fd);
    });

    //监听连接关闭事件$serv->on('close', function ($serv, $fd) {
        echo "Client: Close.\n";
    });

    //启动服务器$serv->start();
Copy after login

4. Start After the service

    php server.php
Copy after login

is started, the cursor will stop here, waiting for other users to connect.

5. Check the connection

Use the command netstat -an | grep port to check whether the port is in the Listening state

    netstat -an | grep 9501
Copy after login

(PS: Pay attention to where the server is The IP address used, if it is the 127.0.0.1 loopback address, the client can only use 127.0.0.1 to connect)

6. Test TCP server

Open a new window and use telnet to connect to the server

    telnet 127.0.0.1 9501
Copy after login

At this time, observe the machine that starts the service and you will find that there is returned data

    php server.php
    > Client:Connect.
Copy after login

When returning to the client, enter hellp world and find that the written and Read successfully

    root@iZ28evegw6zZ:~# telnet 127.0.0.1 9501
    Trying 127.0.0.1...
    Connected to 127.0.0.1.
    Escape character is '^]'.
    hellp world      //此处是输入的命令
    Server:hellp world    //recv()读取命令成功
    Connection closed by foreign host.   //退出成功
    返回到服务器端观察
    root@iZ28evegw6zZ:/var/www/html# php server.php
    Client: Connect.   //连接成功消息
    Receive message: hellp world   //接收到数据
    Client:Close.   //客户端退出成功
Copy after login

The above is the detailed content of Record Swoole study notes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24