How many methods are there for tcp long connection subcontracting?
There are 4 methods for tcp long connection packetization, which are: 1. The method of fixed message length; 2. The method of using special strings as message boundaries; 3. Adding A length field method; 4. A method of subcontracting using the format of the message itself.
Four methods of tcp long connection packetization:
1. Fixed message length
2. Use special Strings as message boundaries. For example, the headers of the http protocol use "\r\n"
as the field separator
3. Add a length field to the header of each message. This is the most common
4. Use the format of the message itself to subpackage. For example, the pairing of <root></root>
in xml, etc.
Related learning recommendations: PHP Programming from beginner to master
Let’s talk about the method of adding message length field to the header for packetization
If each message has a 4-byte header, store the string length. For example, the message "hello", the byte stream is 0x00
,0x00
,0x00
,0x05
,'h',' e','l','l','o'
Write a piece of pseudo code to show subcontracting:
void OnMessage() { while(接收缓冲buf可读字节>=头部长度4) { len = 包的长度 if(len>65536 or len<0) { 关闭连接 } else if(buf>=len+4) { 从buf中提取出4字节; message=将buf后的len长度内存赋给message messagecallback(message)调用用户代码处理message 从buf中移除len长度 } else { 不够一个包的长度,等待下次消息到来,break; } } }
The above is the detailed content of How many methods are there for tcp long connection subcontracting?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools