TCP, UDP, long connection and short connection in Swoole

TCP service

swoole document - TCP service

tcp server

<?php
// 1. create a synchronized blocking tcp service by default
$host = "0.0.0.0"; // 0.0.0.0 means answer all
// The default is tcp
$serv = new Swoole\Server($host, 9000);
// Add configuration
$serv->set([
  'heartbeat_idle_time' => 10,
  'heartbeat_check_interval' => 3,
]);
// 2. registration event
$serv->on('Start', function($serv) use($host){
    echo "start-up swoole Monitoring Information  tcp:$host:9000\n";
});

//Listen for connection entry events
$serv->on('Connect', function ($serv, $fd) {
    echo "Client: Connection successful.\n";
});

//Listening for data receiving events
$serv->on('Receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, "Server: ".$data);
});

//Listening for connection closing events
$serv->on('Close', function ($serv, $fd) {
    echo "Disconnect.\n";
});
// 3. start the server
// block
$serv->start(); // Blocking and non blocking

  

This creates a TCP server that listens to the native 9000 port.

Then start the service first, and then try to connect through the telnet command

 php swoole_tcp_server.php
 # Start the information monitored by spoole tcp:0.0.0.0:9000

 telnet 127.0.0.1 9000
# Trying 127.0.0.1...
# Connected to localhost.

  

tcp client

<?php
// Sync client
$client = new swoole_client(SWOOLE_SOCK_TCP);

//Connect to server
if (!$client->connect('127.0.0.1', 9000, 0.5))
{
    die("connect failed.");
}

function order()
{
     sleep(4);// Suppose some operations take a long time
     return "order\n";
}

//Send data to server
if (!$client->send(order()))
{
    die("send failed.");
}
//Receive data from server
$data = $client->recv();
if (!$data)
{
    die("recv failed.");
}

//Close connection
$client->close();

// Return results to users
echo 'Order generation succeeded'."\n";

  

Run the tcp client to connect to the server. If the processing takes time, the order will be generated successfully

php swoole_tcp_client.php
# Order generation succeeded

  

UDP service

swoole document - UDP service

udp server

<?php
//Create a Server object, listen to 127.0.0.1:9000 port, and the type is SWOOLE_SOCK_UDP
$serv = new swoole_server("0.0.0.0", 9000, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

//Listening for data receiving events
$serv->on('Packet', function ($serv, $data, $clientInfo) {
    $serv->sendto($clientInfo['address'], $clientInfo['port'], "Server ".$data);
    var_dump($clientInfo);
});
echo "0.0.0.0:9000\n";
//Start server
$serv->start();

  

udp client

<?php
$client = new swoole_client(SWOOLE_SOCK_UDP);
$client->sendTo('127.0.0.1', 9000, 'upd');
// Receiving server information
$data = $client->recv();
var_dump($data);

  

Two protocols

TCP(Transmission Control Protocol): it is a connection oriented, reliable and byte stream based transmission communication protocol.

UDP(User Datagram Protocol): it is a connectionless transport layer protocol that provides simple and unreliable transaction oriented information transmission services.

Unlike TCP Server, UDP Server has no concept of connection. After the Server is started, the client can directly send packets to the 9502 port that the Server listens to without connecting.

Long connection, short connection

  • Long connection: the client and the server do not disconnect after establishing a connection. Later, when the client accesses the content on the server again, it continues to use this connection channel.
  • Short connection: the client and the server establish a connection and disconnect immediately after sending data. The next time you want to fetch data, you need to establish a connection again.

Difference between Http long connection and TCP long connection

The difference between Http long connections and TCP long connections is that TCP long connections need to maintain a set of heartbeat policies., Http only needs to add keep alive: true in the request header to realize long connection.

Tags: PHP udp tcp swoole

Posted by vdubdriver on Tue, 31 May 2022 13:53:41 +0530