search
HomePHP FrameworkSwooleRecord Swoole study notes

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. If there is any infringement, please contact admin@php.cn delete
How do I extend Swoole with custom modules?How do I extend Swoole with custom modules?Mar 18, 2025 pm 03:57 PM

Article discusses extending Swoole with custom modules, detailing steps, best practices, and troubleshooting. Main focus is enhancing functionality and integration.

How can I use Swoole's memory pool to reduce memory fragmentation?How can I use Swoole's memory pool to reduce memory fragmentation?Mar 17, 2025 pm 01:23 PM

The article discusses using Swoole's memory pool to reduce memory fragmentation by efficient memory management and configuration. Main focus is on enabling, sizing, and reusing memory within the pool.

How does Swoole's reactor model work under the hood?How does Swoole's reactor model work under the hood?Mar 18, 2025 pm 03:54 PM

Swoole's reactor model uses an event-driven, non-blocking I/O architecture to efficiently manage high-concurrency scenarios, optimizing performance through various techniques.(159 characters)

How do I configure Swoole's process isolation?How do I configure Swoole's process isolation?Mar 18, 2025 pm 03:55 PM

Article discusses configuring Swoole's process isolation, its benefits like improved stability and security, and troubleshooting methods.Character count: 159

How can I contribute to the Swoole open-source project?How can I contribute to the Swoole open-source project?Mar 18, 2025 pm 03:58 PM

The article outlines ways to contribute to the Swoole project, including reporting bugs, submitting features, coding, and improving documentation. It discusses required skills and steps for beginners to start contributing, and how to find pressing is

What Are the Key Benefits of Using Swoole for IoT Applications?What Are the Key Benefits of Using Swoole for IoT Applications?Mar 12, 2025 pm 05:04 PM

This article examines Swoole's benefits for IoT applications. Swoole's asynchronous architecture addresses challenges like high concurrency and real-time demands, improving performance, scalability, and resource utilization compared to traditional m

What Are the Key Features of Swoole's Built-in WebSocket Client?What Are the Key Features of Swoole's Built-in WebSocket Client?Mar 14, 2025 pm 12:25 PM

Swoole's WebSocket client enhances real-time communication with high performance, async I/O, and security features like SSL/TLS. It supports scalability and efficient data streaming.

How can I use Swoole to build a microservices architecture?How can I use Swoole to build a microservices architecture?Mar 17, 2025 pm 01:18 PM

Article discusses using Swoole for microservices, focusing on design, implementation, and performance enhancement through asynchronous I/O and coroutines.Word count: 159

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.