[Introduction to swoole] How to quickly create a tcp server

little bottle
Release: 2023-04-06 07:22:01
forward
2541 people have browsed it

Swoole is a PHP advanced web development framework that can improve website development efficiency. In this article, the editor will introduce how to use swoole to create a tcp server. Friends who are interested can learn it!

server.php


##

<?php/**
 * 创建tcp服务器
 * Date: 2019/1/15 */$serv = new swoole_server(&#39;127.0.0.1&#39;, 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);
});// 监听连接关闭事件$serv->on('close', function ($serv, $fd) {    echo "Client: Close.\n";
});// 启动服务器$serv->start();
Copy after login
1. Execute the program and start the server


$  /usr/local/php/bin/
Copy after login
2. After successful startup, check netstat


$ sudo netstat -ntlp | grep php     
tcp        0      0 127.0.0.1:9501          0.0.0.0:*               LISTEN      21314/php
Copy after login
 3. Telnet to connect to the server

##

$ telnet 127.0.0.1 9501Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.hello
Server: hello
Copy after login

Exit telnet: shift], quit

 4. End the working process: kill main process ID

$ kill 21314
Copy after login

Related tutorials:

PHP video tutorial

Swoole interpretation video tutorial

The above is the detailed content of [Introduction to swoole] How to quickly create a tcp server. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template