Home  >  Article  >  Backend Development  >  How to use swoole to create a server (Part 1)

How to use swoole to create a server (Part 1)

不言
不言Original
2018-07-11 11:39:512156browse

This article mainly introduces one of the swoole creation servers, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

1. Create tcp server

First create a tcp server according to the document

<?php //创建服务器 参数分别是 
//监听的地址 127.0.0.1 表示监听本机,0.0.0.0表示监听所有地址
//9501 端口号 这个你随便定都行只要没被占用过(netstat -an | grep 查看端口使用情况) 如果被占用就使用 命令 kill杀死进程
//SWOOLE_PROCESS 为多进程模式,一般这里不传值,默认为多进程 SWOOLE_BASE为基本模式 
//SWOOLE_SOCK_TCP 也就是说创建tcp服务器 
$serv=new swoole_server(&#39;127.0.0.1&#39;,&#39;9501&#39;,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);

//服务器设置参数
$serv->set([
'worker_num'=>4,  //worker进程数 一般为cpu数的 1-4倍
'max_request'=>10000, //worker进程在处理完n次请求后结束运行重建一个
]);
//这里
//当然还有很多设置的参数 文档传送门:https://wiki.swoole.com/wiki/page/13.html

//监听连接进入事件(这里是一个闭包写法)连接后调用的函数
//$fd客户端连接的唯一标示
//$reactor_id 线程id 是一个自增数字,范围是1 ~ 1600万,fd超过1600万后会自动从1开始进行复用
$serv->on('connect', function ($serv, $fd, $reactor_id) {
    echo "Client: {$reactor_id} - {$fd}-Connect.\n";
});

//监听数据接收事件
//$data就是接收的数据
$serv->on('receive', function ($serv, $fd, $reactor_id, $data)) {
    $serv->send($fd, "Server: ".$data);
    echo '收到了数据'.$data;
});

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

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

ok let’s test php tcp.php to open the tcp server (if the port is occupied, kill the process occupying the port and turn off the use ctrl c, use ctrl z to suspend)
How to use swoole to create a server (Part 1)

Open telnet (we need to open another terminal) to test whether it is successful, just enter eqqeq

How to use swoole to create a server (Part 1)

Look at the tcp server again

How to use swoole to create a server (Part 1)

Let’s sort out our thoughts when we see this:
Created the tcp server-> ;telnet connects to the server->tcp executes connect->telnent sends information->tcp listens to the receive event->telnent disconnects->tcp listens to the close event

question
1.The relationship between server and telnet
The relationship between server and client, one server corresponds to multiple telnet

2.$serv->send($fd, "Server: ".$data); What is the difference between directly outputting echo?

send is to send data to the other party (connected to my server). For example, if you use WeChat to chat with your friends, and you send a server: ".$data, then it is equivalent to sending the message Sent to your friend, and the echo output content is only for yourself.

3. What are threads and processes?
Equivalent to the threads in a factory (CPU) Multiple workshops (processes)
A workshop (process) has many workers (threads)
There is a toilet (shared memory) in the workshop, which can be shared by all workers, but you have to wait until the workers have finished using it before you can go in

We usually use PHP in a single process. For such a large factory, it is of course slow to use one workshop.
So we need multiple workshops (multi-process) and multiple employees (threads) to improve efficiency

Note:
Every time you modify the server script file, you must
close the process and reopen it for it to take effect

2. Create a udp server

1.TCP core ucp are all transport layer protocols, but UDP server is different from TCP server. UDP has no concept of connection. UDP consumes relatively small resources but is fast. After starting the Server, the client can directly send data packets to the 9502 port monitored by the Server without Connect.

2.udp is created in a way similar to tcp

//就是把最后一个参数改为 SWOOLE_SOCK_UDP 
$serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

3.UDP server can be used Instead of telnet, use netcat -u to connect and test
nc installation:
yum -y install yum -y install nc.x86_64 Use it directlync -u 127.0.0.1 9502

4. Complete implementation

//创建Server对象,监听 127.0.0.1:9502端口,类型为SWOOLE_SOCK_UDP
$serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
 
//udp没有 $serv->on connect的概念

//监听数据接收事件
$serv->on('Packet', function ($serv, $data, $clientInfo) {
    $serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
    var_dump($clientInfo);
});

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

5. Test, we also open 2 terminals
udp server Here we printed $clientInfo

How to use swoole to create a server (Part 1)

nc test server

How to use swoole to create a server (Part 1)


ok The test is no problem^-^

The above is the entire content of this article, I hope It will be helpful for everyone’s learning. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Swooled Learning - Introduction to Swoole

The above is the detailed content of How to use swoole to create a server (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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