Home > PHP Framework > Swoole > Record Swoole study notes

Record Swoole study notes

coldplay.xixi
Release: 2021-01-22 10:07:25
forward
2203 people have browsed it

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!

Related labels:
source:csdn.net
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template