Share a php tcp packet sticking/unpacking example

藏色散人
Release: 2023-04-11 09:00:01
forward
6508 people have browsed it

This article will introduce you to an example about PHP's tcp sticking/unpacking. I hope it will be helpful to friends who need it~

Share a php tcp packet sticking/unpacking example

In tcp long link mode, Use a fixed message header length method to unpack messages to solve the sticky problem. The fixed message header protocol

fixes the first N bytes of the message header as the message length bits. Combined with the business scenario, 2bytes or 4bytes, when reading the message, first read the message length bits, and then according to the specific message The length reads the message content. [Recommended learning: PHP video tutorial]

pack/unpack can pack values ​​into binary/unpack binary into values. For specific modes, please refer to the detailed usage of pack/unpack. Here we choose fixed The header length is 2 bytes to represent the length of the message body. The maximum length of the message body can be represented by 2^16 - 1. If it is not enough, you can use 4 bytes.

Group package

<?php
// msg protocol
// | ---- dataLen ---- | data |
// | - fixed 2bytes - |

// 模拟客户端连续发送2条消息
$foo = "hello world";
$bar = "i am sqrt_cat";

$package = "";

// 使用 n 打包 固定2bytes
$fooLenn = pack("n", strlen($foo));
$package = $fooLenn . $foo;

$barLenn = pack("n", strlen($bar));
$package .= $barLenn . $bar;
Copy after login

sticky package

// send
// 传输 $package 由 $foo $bar 两条消息组成 模拟粘包场景
// receive
Copy after login

Unpacking

<?php
// 解析第1条消息 取前 2bytes 按 n 解包
$fooLen = unpack("n", substr($package, 0, 2))[1];
// 使用包消息体长度定义读取消息体
// 从第 3byte 开始读 前 2bytes表示长度
$foo = substr($package, 2, $fooLen);
echo $foo . PHP_EOL;

// 解析第2条消息 取前 2bytes 按 n 解包
// 0 ~ (2 + fooLen) - 1 字节序为 fooLen . foo
// (2 + fooLen) ~ (2 + fooLen) + 2 - 1 为 barLen
$barLen = unpack("n", substr($package, (2 + $fooLen), 2))[1];
$bar    = substr($package, (2 + $fooLen) + 2, $barLen);
echo $bar . PHP_EOL;
Copy after login

The TCP scenario often encountered in daily work may be the short connection single message mode. The client closes the connection after sending a message, and the server reads EOF in a loop to get a complete message. However, if there are multiple messages in a short connection or in a long link mode, sticky packets may occur, and the client cannot confirm that the message has been read through EOL without closing the server. This requires defining protocols and unpacking.

The above is the detailed content of Share a php tcp packet sticking/unpacking example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:learnku.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template