This article mainly introduces about Swoole to create a server, has a certain reference value, now share to everyone, the need for friends can refer to
1. Create a TCP server
Create a TCP server according to the document first
<?php//Create server parameters are//listen to the address 127.0.0.1 for listening to this machine, 0.0.0.0 means to listen to all addresses//9501 port number This is what you do as long as it's not occupied (Netstat-an | grep view port usage) if Use command kill to kill the process//swoole_process is a multi-process mode, generally does not pass value, the default is multi-process swoole_base for the basic mode//swoole_sock_tcp that is to create a TCP server $serv =new Swoole_server (' 127.0.0.1 ', ' 9501 ', swoole_process,swoole_sock_tcp);//server Setup parameters $serv->set ([' Worker_num ' =>4,// The number of worker processes is typically 1-4 times the number of CPUs ' max_request ' =>10000, the//worker process ends up running after processing n requests (one]);//here//Of course there are a lot of parameters to set the document Portal: https:// wiki.swoole.com/wiki/page/13.html//Listener Connection Entry event (here is a closure method) The function that is called after the connection//$FD Client connection is unique//$reactor the _id thread ID is a self-increment number with a range of 1 ~ 16 million, FD more than 16 million will automatically start from 1 for Multiplexing $serv->on (' Connect ', function ($serv, $FD, $reactor _id) {echo "Client: {$reactor _id}-{ $FD}-connect.\n ";}); /Listen data receive Event//$data is the received data $serv->on (' Receive ', function ($serv, $FD, $reactor _id, $data)) {$serv->send ($fd, "Serve R: ". $data); Echo ' received the data '. $data;}); /Listen for connection Shutdown Event $serv->on (' Close ', function ($serv, $FD) {echo "Client:close". $fd. " \ n ";}); /boot server $serv->start ();
OK let's test PHP tcp.php open the TCP server (if the port is occupied, kill the process that occupies the port, turn off using CTRL + C, hang with Ctrl + Z)
Open Telnet again (we're going to open a terminal again) to test if it's successful, just type Eqqeq.
Then look at the TCP server
See here we tidy up the idea:
Create a TCP server->telnet connection server->tcp perform connect->telnent send message->tcp hear receive event->telnent disconnect->tcp listen to the Close event
Questions
1.server Relationship to Telnet
Server-to-customer relationship, one server for multiple Telnet
2. $serv->send ($FD, "Server:". $data); What's the difference between a direct output echo?
Send sends data to each other (connecting to my server), such as when you chat with your friends, you send a server: ". $data, then it's the equivalent of sending the message to your friend, and Echo is outputting the content, just to yourself."
3. Threads and processes What's the ghost?
Equivalent to multiple workshops (processes) in one factory (CPU)
A workshop (process) has a lot of workers (threads)
There is a toilet in the workshop (shared memory), the workers can be shared, but you have to wait until the workers run out before they can go in
We usually use PHP is a single process, then the large factory used a workshop efficiency of course slow,
So we need multiple workshops (multi-process), multiple employees (threads), to improve efficiency
Attention:
Each time you modify the server script file,
Close the process and reopen it to take effect
2. Creating a UDP server
1.TCP kernel UCP is the Transport layer protocol, but the UDP server differs from the TCP server, UDP does not have the concept of connection, UDP consumes less resources, but it is fast. After starting the server, the client does not need connect and can send packets directly to the server listening on port 9502.
2.UDP is created in the same way as TCP.
is to change the last parameter to swoole_sock_udp $serv = new Swoole_server ("127.0.0.1", 9502, Swoole_process, SWOOLE_SOCK_UDP);
3.UDP server can use Netcat-u to connect tests without Telnet
NC Installation:
yum-y Install yum-y install nc.x86_64 use is direct nc-u 127.0.0.1 9502
4. Complete implementation
Create the server object, listen for the 127.0.0.1:9502 port, type Swoole_sock_udp$serv = new Swoole_server ("127.0.0.1", 9502, Swoole_process, SWOOLE_SOCK_UDP); UDP does not $serv the concept of->on connect//Listen data receive event $serv->on (' Packet ', function ($serv, $data, $clientInfo) { $serv SendTo ($clientInfo [' address '], $clientInfo [' Port '], "Server". $data); Var_dump ($clientInfo);}); /boot server $serv->start ();
5. Test, also we open 2 terminals
UDP server Here we have printed $clientinfo
NC Test Server
OK Test No problem ^-^
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!