Method for realizing communication by taking message as unit on basis of TCP (Transmission Control Protocol)

By adopting the communication method based on messages in the communication method of the TCP protocol, the encoding and decoding process is simplified, and the problem of frequent replacement of codecs in different software systems is solved, thereby achieving efficient and stable message security communication.

CN119835347APending Publication Date: 2025-04-15CHANGCHUN JINGYI PHOTOELECTRIC TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510044735.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-10
Publication Date
2025-04-15

AI Technical Summary

Technical Problem

During the existing TCP communication development process, the encoding and decoding process is complicated and error-prone, and the user protocols of different software systems are different, which makes it difficult to rewrite the codec code every time.

Method used

Using a message-based communication method based on TCP protocol, the encapsulation module and decoding module are set up separately on the sender and the receiver to simplify the encoding and decoding process and realize the encapsulation and decoding of data packets.

Benefits of technology

The encoding and decoding process of TCP protocol is simplified, and the trouble of rewriting user protocols is avoided in different software systems every time, and secure communication based on messages is realized, which improves development efficiency and stability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119835347A_ABST
    Figure CN119835347A_ABST
Patent Text Reader

Abstract

The invention relates to a method for realizing communication by taking a message as a unit on the basis of a TCP (Transmission Control Protocol), which comprises the following steps that: in a sender application program layer, a sender user calls a sending function and takes user data as a sending function parameter to be substituted into a sender packaging module; in a sender packaging module, packaging a preset data initial positioning byte, data volume length information and user data into a data packet, and sending the data packet to network equipment; after a receiving party decoding module receives the data, firstly searching a data initial positioning byte in the data to obtain data volume length information, and circularly receiving the data; then a complete data packet is assembled; deleting data initial positioning bytes and data body length information; a data receiving signal is sent to a receiver application program layer, and the user data BUFA is sent to the receiver application program layer. According to the invention, a secure transmission mode taking a message as a unit is realized, the problem of packet connection is eliminated, and the coding and decoding process of communication by using a TCP protocol is simplified.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the technical field of information communication, and relates to a communication method based on TCP protocol and taking messages as units. Background Art

[0002] The existing TCP communication development process first designs the application layer communication protocol. The sender user writes the data header (including the data start positioning byte and the data body length byte) according to the communication protocol and assembles it with the user data into a message data. The data header contains two parts. In order to prevent the large amount of message data from causing a sending timeout, each assembled message data needs to be divided into blocks and placed in the sending buffer; then the receiving user receives the message data in a loop, and parses the message data according to the protocol while receiving it, until a group of user data is collected and processed; the sender user and the receiving user continue to assemble and receive and parse the next user data. Under the above conditions, every time a new TCP protocol user data is written, the data start positioning byte and the data body length byte must be written and assembled with the user data into multiple message data. These codes are complex to write, prone to errors, and difficult to debug; if the user protocol remains unchanged, each process is complex, but it only needs to be written once and debugged. The complexity is that different software systems have different user protocols, which causes this encoding and decoding process to need to be rewritten each time, and each time it is different. The current situation is that in different software systems, the user protocols are different for each system, and there is no unified regulation. This causes the encoding when sending data and the decoding when receiving data to need to be rewritten. Rewriting the codec each time is time-consuming and laborious. At the same time, it is also a cumbersome process for the receiving user to receive data of the TCP protocol. The basic work includes finding the starting positioning byte of the data and decoding the data body length information in the data header. This length information indicates the byte length of the subsequent user data. The user data is continuously received according to this length until the data length is met. The number of cycles and the time consumed by this continuous loop reception are uncertain, which is determined by the data length and network speed. If the network connection is unstable, it may be disconnected during this process. Summary of the invention

[0003] The technical problem to be solved by the present invention is to provide a communication method based on the TCP protocol to realize message-based communication. The method simplifies the encoding and decoding process of communication using the TCP protocol and can realize secure communication in message-based units.

[0004] In order to solve the above technical problems, the present invention implements a communication method based on the TCP protocol in a message unit as follows:

[0005] At the sender application layer, the sender user calls the sending function sendpkt set in the sender encapsulation module, and substitutes the user data BUF_A into the sender encapsulation module as the sending function parameter; in the sender encapsulation module, the preset data start positioning byte, the data body length information representing the length of the user data BUF_A, and the user data BUF_A are encapsulated into a data packet BUF_B and sent to the network device; after receiving the data from the network device, the receiving decoding module first searches for the data start positioning byte in the data to obtain the data body length information, and then cyclically receives the subsequent data according to the data body length information until enough data corresponding to the data body length information is received; then the data start positioning byte, the data body length information and the subsequent cyclically received data are assembled into a complete data packet BUF_B; the data start positioning byte and the data body length information are deleted; a data reception signal is sent to the receiving application layer, and the user data BUF_A is delivered to the receiving application layer.

[0006] Furthermore, when the user data BUF_A exceeds the set length threshold, the data packet BUF_B is divided into a plurality of data blocks and first sent to the buffer area, and then sent to the network device in sequence.

[0007] Furthermore, the data start positioning byte occupies two bytes, and the data body length information occupies four bytes.

[0008] Furthermore, the data body length information uses unsigned integer data with the little end first to represent the length of the user data BUF_A.

[0009] Beneficial effects:

[0010] 1. The present invention refers to the message mode transmission characteristics of the UDP protocol, combines the advantages of the TCP protocol and the UDP protocol, realizes a secure transmission method based on messages, and optimizes the algorithm to eliminate the problem of UDP protocol packet concatenation.

[0011] 2. The present invention introduces the "message" mechanism into the TCP communication process, which simplifies the encoding and decoding process of using the TCP protocol for communication. After adopting the present invention, there is no need to rewrite the user protocol in different software systems, thus eliminating the encoding and decoding process. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] Figure 1 It is a flow chart of the present invention.

[0013] Figure 2 This is a schematic diagram of the format of data packet BUF_B.

[0014] Figure 3 This is a schematic diagram of the user data BUF_A format. DETAILED DESCRIPTION

[0015] The present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention rather than to limit the present invention.

[0016] like Figure 1 As shown, the communication method based on the TCP protocol of the present invention is implemented in a message unit as follows:

[0017] At the sender application layer, the sender user calls the sending function sendpkt set in the sender encapsulation module, and substitutes the user data BUF_A into the sender encapsulation module as the sending function parameter; in the sender encapsulation module, the preset data start positioning byte, the data body length information representing the length of the user data BUF_A, and the user data BUF_A are encapsulated into a data packet BUF_B and sent to the network device; the data start positioning byte and the data body length information constitute the communication data header; in the case where the user data BUF_A is too long, the data packet BUF_B is divided into multiple data blocks and first sent to the buffer area, and then sent to the network device in sequence.

[0018] For example, the user data BUF_A that the user needs to send based on the TCP protocol is a 10-byte string "0123456789":

[0019] First, the sender user calls the sending function sendpkt set in the sender encapsulation module and substitutes the string "0123456789" as the sending function parameter into the sender encapsulation module;

[0020] The data start positioning byte preset in the sender encapsulation module occupies 2 bytes, which are 0x55 and 0xAA respectively. The data body length information is a little-endian unsigned integer data representing the byte length occupied by the user data BUF_A. The communication data header format is shown in Table 1.

[0021] Table 1

[0022]

[0023] Without using the present invention, the user protocols defined by developers of different software systems are different, and the protocol headers are also different. This is why different software needs to rewrite the codec. The advantage of using the present invention is not reflected in one software system. This method can be used in the development of different software systems, so that developers do not need to rewrite the codec process). In the sender encapsulation module, the communication data header and user data BUF_A are encapsulated into a data packet BUF_B and sent to the network device. The encapsulated data packet BUF_B is as follows: Figure 2 shown.

[0024] When the user data BUF_A exceeds the set length threshold, for example, the user data BUF_A is a string of 100M bytes "0123456789...", in the sender encapsulation module, the data start positioning bytes 0x55, 0xAA, representing the data body length information of the user data BUF_A occupying 100M bytes and the user data BUF_A is encapsulated into a data packet BUF_B, and then the data packet BUF_B is divided into multiple data blocks and sent to the buffer area (only the first data block has the communication data header), and then the multiple data blocks are sent to the network device in sequence. There is no strict requirement for the size of the length threshold, which needs to be determined based on multiple factors such as network speed, timeout time, buffer size, etc.

[0025] The receiver decoding module receives data from the network device, parses the data according to the protocol, assembles the complete data packet BUF_B, deletes the communication data header to restore the user data BUF_A, sends a data reception signal to the receiver application layer, and delivers the user data BUF_A to the receiver application layer, as follows:

[0026] After receiving the data from the network device, the receiving decoding module first searches for the data start positioning byte in the data to obtain the data body length information, and then cyclically receives the subsequent data according to the data body length information until it receives enough data with bytes corresponding to the data body length information; then it assembles the data start positioning byte, the data body length information and the subsequent cyclically received data into a complete data packet BUF_B; after deleting the data start positioning byte and the data body length information, it retains the user data BUF_A; sends a data reception signal to the receiving application layer, and delivers the user data BUF_A to the receiving application layer.

[0027] Taking the user data BUF_A as a 10-byte string "0123456789" as an example, after the receiving decoding module receives the data from the network device, it first searches for the data start positioning bytes 0x55 and 0xAA in the data, obtains the data body length information (10 bytes), and cyclically receives the following 10 bytes of data; the assembled complete data packet BUF_B is 0x55+0xAA+data body length information+user data BUF_A; delete the communication data header "0x55+0xAA+data body length information, and the format of the retained user data BUF_A is shown in Figure 3 , which is displayed as "0123456789" according to ASCII data; a data reception signal is sent to the receiving application layer, and the user data BUF_A is delivered to the receiving application layer, thus finally realizing communication in message units.

[0028] When the user data BUF_A is too long, taking the user data BUF_A as a string of 100M bytes "0123456789..." as an example, the receiving decoding module first searches for the data start positioning bytes 0x55 and 0xAA in the data, obtains the data body length information (100M bytes), and cyclically receives the following 100M bytes of data; the assembled complete data packet BUF_B is 0x55+0xAA+data body length information+user data BUF_A; deletes the communication data header "0x55+0xAA+data body length information", retains the user data BUF_A, and finally sends a data reception signal to the receiving application layer to deliver the user data BUF_A to the receiving application layer.

[0029] The present invention presets a fixed data start positioning byte in a sender encapsulation module, and encapsulates the data start positioning byte, the data body length information representing the length of user data BUF_A, and the user data BUF_A into a data packet BUF_B in the sender encapsulation module. The data encoding and sending processes are all completed in the sender encapsulation module, and there is no need to rewrite the data header every time a new TCP protocol user data is written. The encoding is simple and not prone to errors. At the same time, the receiving and decoding processes are completed in the receiver decoding module. The present invention greatly simplifies TCP communication and can realize a secure communication function based on messages. This function can be referred to as "what you send is what you get". "What you send is what you get" means that each time the sender calls a sending function to send data, the sent data will be received completely and once, and no error bytes will appear. The data encapsulation and decoding processes of the present invention are all implemented with program modules, which is convenient for users to call.

[0030] The UDP protocol uses messages (messages: a block of continuous byte data sent at a time) as units. Each sent data is called a message, and the receiving end receives it with messages as the smallest unit, which reduces the trouble of assembling and unpacking. However, the UDP protocol is an unsafe transmission protocol and cannot guarantee the correctness of transmission.

[0031] The workflow of the sender encapsulation module and the receiver decoding module is implemented using the QT environment.

Claims

1. A communication method based on TCP protocol and taking messages as units, characterized in that The method is as follows: At the sender application layer, the sender user calls the sending function sendpkt set in the sender encapsulation module, and substitutes the user data BUF_A into the sender encapsulation module as the sending function parameter; in the sender encapsulation module, the preset data start positioning byte, the data body length information representing the length of the user data BUF_A, and the user data BUF_A are encapsulated into a data packet BUF_B and sent to the network device; after receiving the data from the network device, the receiving decoding module first searches for the data start positioning byte in the data to obtain the data body length information, and then cyclically receives the subsequent data according to the data body length information until enough data corresponding to the data body length information is received; then the data start positioning byte, the data body length information and the subsequent cyclically received data are assembled into a complete data packet BUF_B; the data start positioning byte and the data body length information are deleted; a data reception signal is sent to the receiving application layer, and the user data BUF_A is delivered to the receiving application layer.

2. The communication method based on the TCP protocol according to claim 1 is characterized in that When the user data BUF_A exceeds the set length threshold, the data packet BUF_B is divided into multiple data blocks and first sent to the buffer area, and then sent to the network device in sequence.

3. The communication method based on the TCP protocol according to claim 1 is characterized in that The data start positioning byte occupies two bytes, and the data body length information occupies four bytes.

4. The communication method based on the TCP protocol according to claim 1, characterized in that The data body length information uses unsigned integer data with the little end first to represent the length of the user data BUF_A.