Home >PHP Framework >Swoole >Record Swoole study notes

Record Swoole study notes

coldplay.xixi
coldplay.xixiforward
2021-01-22 10:07:252215browse

Record Swoole study notes

Recommended (free): swoole

1. Installation

Currently there are two officially recommended methods

1). Use pecl to install

pecl install swoole

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

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

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();

4. Start After the service

    php server.php

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

(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

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

    php server.php
    > Client:Connect.

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.   //客户端退出成功

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:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete