TCP, UDP and long and short connections in Swoole

TCP service

swoole documentation-TCP service

tcp server

<?php
// 1. 创建 swoole 默认创建的是一个同步的阻塞tcp服务
$host = "0.0.0.0"; // 0.0.0.0 代表接听所有
// 默认是tcp
$serv = new Swoole\Server($host, 9000);
// 添加配置
$serv->set([
  'heartbeat_idle_time' => 10,
  'heartbeat_check_interval' => 3,
]);
// 2. 注册事件
$serv->on('Start', function($serv) use($host){
    echo "启动 swoole 监听的信息tcp:$host:9000\n";
});

//监听连接进入事件
$serv->on('Connect', function ($serv, $fd) {
    echo "Client: 连接成功.\n";
});

//监听数据接收事件
$serv->on('Receive', function ($serv, $fd, $from_id, $data) {
    $serv->send($fd, "Server: ".$data);
});

//监听连接关闭事件
$serv->on('Close', function ($serv, $fd) {
    echo "断开连接.\n";
});
// 3. 启动服务器
// 阻塞
$serv->start(); // 阻塞与非阻塞

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

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

 php swoole_tcp_server.php
 # 启动 swoole 监听的信息tcp:0.0.0.0:9000

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

tcp client

<?php
// 同步客户端
$client = new swoole_client(SWOOLE_SOCK_TCP);

//连接到服务器
if (!$client->connect('127.0.0.1', 9000, 0.5))
{
    die("connect failed.");
}

function order()
{
     sleep(4);// 假设某一些操作造成时间很长
     return "order\n";
}

//向服务器发送数据
if (!$client->send(order()))
{
    die("send failed.");
}
//从服务器接收数据
$data = $client->recv();
if (!$data)
{
    die("recv failed.");
}

//关闭连接
$client->close();

// 返回结果给用户
echo '订单生成成功'."\n";

Run the tcp client to connect to the server, assuming that the processing takes time, and finally it will output that the order is generated successfully

php swoole_tcp_client.php
# 订单生成成功

UDP service

swoole documentation-UDP service

udp server

<?php
//创建Server对象,监听 127.0.0.1:9000端口,类型为SWOOLE_SOCK_UDP
$serv = new swoole_server("0.0.0.0", 9000, SWOOLE_PROCESS, SWOOLE_SOCK_UDP);

//监听数据接收事件
$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";
//启动服务器
$serv->start();

udp client

<?php
$client = new swoole_client(SWOOLE_SOCK_UDP);
$client->sendTo('127.0.0.1', 9000, 'upd');
// 接收服务端信息
$data = $client->recv();
var_dump($data);

Two agreements

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

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

UDP server is different from TCP server, UDP has no concept of connection. After starting the server, the client does not need to connect, and can directly send data packets to the 9502 port monitored by the server.

Long connection, short connection

  • Long connection: The client and the server will not be disconnected after the connection is established. After the client accesses the content on the server again, it will continue to use this connection channel.
  • Short connection: The client and the server establish a connection, and immediately disconnect after sending the data. Next time you want to fetch data, you need to establish a connection again.

The difference between Http long connection and TCP long connection

The difference between Http long connection and TCP long connection is: TCP long connection needs to maintain a set of heartbeat strategy. , And Http only needs to keep-alive:truebe added in the request header to achieve a long connection.

 

My official group click here to join the group chat [PHP/web/advanced learning exchange group] , learn together and discuss with each other.

The group has already managed to organize the knowledge system ( source code, learning videos, etc. ), welcome to join the group to receive it for free .

This set of high-quality PHP tutorials is by no means comparable to those coquettish bastards on the market. As a leader in web development, PHP is not inferior to other languages. With the addition of Swoole , it is even more powerful! Enter the communications and Internet of Things industries to develop Baidu Maps, Baidu Order Center, Huya, Zhanqi TV, etc.! After the cold winter layoffs period is the period for major companies to expand recruitment. Now the market is flooded with junior programmers. Advanced middle and senior programmers are definitely the talents urgently needed by major companies. This set of learning tutorials is suitable for those PHP within 1-5 years. Developers are in a bottleneck period and want to break through their advanced mid-senior, architects! Places are limited, first come first served!

Tencent T3-T4 standard boutique PHP architect tutorial catalogue, as long as you read it to ensure that the salary rises to a higher level (continuous update)​

Partial data screenshot:

 

 

 

 

Limited-time premium benefits:

★Tencent senior PHP engineer written test questions

★Processing of orders for billion-level PV high concurrency scenarios

★ Laravel develops Tmall component service

★Zhanqi TV video live broadcast architecture project actual combat

Scan the QR code below to receive

 

 

For friends who are interested in PHP back-end technology and PHP architecture technology, click here for my official group to learn together and discuss with each other.

The group has already managed to organize the knowledge system (source code, learning videos, etc.), welcome to join the group to receive it for free.

This course is in-depth benchmarking with Tencent T3-T4 standards, creating a personal learning plan for web developers to advance to mid-to-senior level, architects to upgrade their skills, and to increase their salaries for themselves! Joining the BAT special training camp can also get the quota of internal promotion and GO language learning permissions! ! !

Guess you like

Origin blog.csdn.net/weixin_43814458/article/details/107083730