How to create a server

Time:2021-5-4

catalogue

Continuous update…

1. Create TCP server

First, create a TCP server according to the document

<?php
//The create server parameters are 
//The address 127.0.0.1 means to monitor the local machine, and 0.0.0.0 means to monitor all addresses
//You can choose the port number of 9501 as long as it is not occupied (netstat - an | grep check the port usage). If it is occupied, use the command kill to kill the process
//SWOOLE_ Process is a multi process mode. Generally, no value is passed here. The default is multi process zoom_ Base is the basic mode 
//SWOOLE_ SOCK_ TCP is to create a TCP server 
$serv=new swoole_server('127.0.0.1','9501',SWOOLE_PROCESS,SWOOLE_SOCK_TCP);

//Server setting parameters
$serv->set([
'worker_ Num '= > 4, // worker process number is generally 1-4 times of CPU number
'max_ Request '= > 10000, // worker process finishes running after processing n requests and reconstructs one
]);
//Here
//Of course, there are also many parameter document gateways https://wiki.swoole.com/wiki/page/13.html

// monitor the connection entry event (here is a closure method) function after connection.
//Unique identifier of $FD client connection
//$reactor_ ID thread ID is a self increasing number, ranging from 1 to 16 million. If FD exceeds 16 million, it will be reused automatically from 1
$serv->on('connect', function ($serv, $fd, $reactor_id) {
    echo "Client: {$reactor_id} - {$fd}-Connect.\n";
});

//Monitoring data receiving events
//$data is the received data
$serv->on('receive', function ($serv, $fd, $reactor_id, $data)) {
    $serv->send($fd, "Server: ".$data);
    Echo 'received data';
});

//Listen for connection closing events
$serv->on('close', function ($serv, $fd) {
    echo "Client: Close".$fd."\n";
});

//Start the server
$serv->start();

OK, let’s test PHP TCP. PHP turns on the TCP server (if the port is occupied, kill the process occupying the port, turn off using Ctrl + C, hang using Ctrl + Z)
How to create a server

Open telnet again (we want to open a terminal again) to test whether it is successful. Enter eqeq casually

How to create a server

Let’s look at the TCP server

How to create a server

Here, let’s sort out our thinking:
TCP server > telnet connection server > TCP execute connect > telnet send message > TCP listen to receive event > telnet disconnect > TCP listen to close event


doubt
1. The relationship between server and telnet
The relationship between the server and the customer service side, a server corresponds to multiple telnets

2.$serv->send($fd, “Server: “.$data); What’s the difference between direct output and echo?

Send is to send data to the other party (connected to my server). For example, if you chat with your friends on wechat, you send a server, “. $data, which is equivalent to sending the message to your friends, and echo is to output the content only to yourself.

3. What are threads and processes?
Equivalent to multiple workshops (processes) in a factory (CPU)
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 for the workers to use up before you can go in

We usually use PHP as a single process, so a large factory uses a workshop. Of course, the efficiency is slow,
So we need multiple workshops (multi process) and multiple employees (threads) to improve efficiency

be careful:
Every time you modify the server script file, you need to
Close the process againReopenWill take effect

2. Create a UDP server

1. TCP core UCP is transport layer protocol, but UDP server is different from TCP serverWithout the concept of connection, UDP consumes less resources, but it is fast. After starting the server, the client can directly send packets to the 9502 port monitored by the server without connecting.

2. UDP is created in a way similar to TCP

//Change the last parameter to zoom_ SOCK_ UDP 
$serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

3. UDP server can use netcat – U instead of Telnet for connection test
NC installation:
yum -y install yum -y install nc.x86_64Use is directnc -u 127.0.0.1 9502

4. Complete implementation

//Create a server object, listen to port 127.0.0.1:9502, and type it as zoom_ SOCK_ UDP
$serv = new swoole_server("127.0.0.1", 9502, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);
 
//UDP does not have the concept of $serv - > on connect

//Monitoring data receiving events
$serv->on('Packet', function ($serv, $data, $clientInfo) {
    $serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
    var_dump($clientInfo);
});

//Start the server
$serv->start();

5. Test, we also open two terminals
Here we print $clientinfo on the UDP server

How to create a server

NC test server

How to create a server


OK, there’s no problem with the test^-^

Thinking: TCP and UDP are both transport layer protocol servers, so what is the application layer server based on transport layer?

If this article is useful to you, you can praise it or collect it as an encouragement to the author~

Document address portal:https://wiki.swoole.com/wiki/…
Recommend teacher singwa’s video tutorial:https://coding.imooc.com/clas…

Also attached is my website…www.entercode.cnPromote a wave of

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]