Custom protocol communication method and system based on TCP

By adopting a custom protocol communication method and system based on TCP between equipment and instruments, the problems of communication scalability, flexibility and inefficiency in the prior art are solved, and efficient and flexible communication protocols are realized, and the personalized needs of diversified equipment are met.

CN119814886APending Publication Date: 2025-04-11FUJIAN NEBULA ELECTRONICS CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411689243.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-25
Publication Date
2025-04-11

Smart Images

  • Figure CN119814886A_ABST
    Figure CN119814886A_ABST
Patent Text Reader

Abstract

The invention provides a user-defined protocol communication method and system based on a TCP in the technical field of communication, and the method comprises the steps: S1, enabling a client to be asynchronously connected with a server, and setting a receiving message callback class for receiving a message of the server; s2, the server side obtains the message content to be transmitted, and a message sending list, a message header, a protocol field, a field type and a protocol type are self-defined; s3, the server side assembles a message header, a protocol field, a field type, a protocol type and message content into a message in a TCP format based on the message sending list, and sends the message to the client side; and step S4, the client customizes a message analysis list, receives the information message sent by the server through the received message callback class, and analyzes the information message through the message analysis list to obtain the message content. The method has the advantages that the expansibility, flexibility and efficiency of communication are greatly improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of communication technology, and in particular to a TCP-based custom protocol communication method and system. Background Art

[0002] In the intelligent environment, equipment and instruments are becoming more diversified and refined. Communication and detection with different equipment and instruments have involved all walks of life, which has brought about various communication protocols adapted to different equipment and instruments. For example, TCP communication has its own set of systems. When adding new equipment and instruments, it is necessary to redevelop the corresponding communication protocol for communication. This is very inconvenient for enterprises with various equipment and instruments, and it takes a certain amount of time to connect, which is inefficient and has a single usage scenario.

[0003] Therefore, how to provide a TCP-based custom protocol communication method and system to improve the scalability, flexibility and efficiency of communication has become a technical problem that needs to be solved urgently. Summary of the invention

[0004] The technical problem to be solved by the present invention is to provide a TCP-based custom protocol communication method and system to improve the scalability, flexibility and efficiency of communication.

[0005] In a first aspect, the present invention provides a TCP-based custom protocol communication method, comprising the following steps:

[0006] Step S1, the client is asynchronously connected to the server, and a message receiving callback class is set for receiving a message message from the server;

[0007] Step S2: The server obtains the content of the message to be transmitted, and customizes a message sending list, a message header, a protocol field, a field type, and a protocol type;

[0008] Step S3: The server assembles the message header, protocol field, field type, protocol type and message content into a message message in TCP format based on the message sending list, and sends the message message to the client;

[0009] Step S4: The client customizes a message parsing list, receives the message message sent by the server through the message receiving callback class, and parses the message message through the message parsing list to obtain the message content to complete the communication.

[0010] Furthermore, the step S1 is specifically as follows:

[0011] The client uses the TcpClient object to access the server IP and server port through asynchronous Socket programming to connect to the server asynchronously, and sets a receive message callback class for receiving the message message from the server.

[0012] Further, in step S2, the message sending list is used to assemble the message message according to a preset format;

[0013] The message header at least includes a first start bit, a first end bit, a command code and a serial number;

[0014] The protocol field includes a special field and a regular field, which are represented by an enumeration type; the special field is used to set a custom identifier; the regular field includes at least a second start bit, a second end bit, a data type, a data field length, a data field, a check bit, a fixed value, a field content, a field value type, and a field length;

[0015] The field type is the type of the protocol field, which is represented by a data type.

[0016] Furthermore, the step S3 is specifically as follows:

[0017] The server encodes the message content into encoded data based on the protocol type, assembles the message header, protocol field, field type, protocol type and encoded data into a byte stream based on the message sending list, generates a message packet in TCP format based on the byte stream and sends it to the client.

[0018] Furthermore, the step S4 is specifically as follows:

[0019] The client customizes a message parsing list for storing the mapping relationship between protocol types and decoding methods, receives the message message sent by the server in real time through the message receiving callback class, parses the message message to obtain a byte stream including a message header, protocol field, field type, protocol type and encoded data, matches the corresponding decoding method from the message parsing list based on the protocol type, decodes the encoded data through the decoding method to obtain the message content, so as to complete the communication.

[0020] In a second aspect, the present invention provides a TCP-based custom protocol communication system, comprising the following modules:

[0021] The asynchronous connection module is used for asynchronous connection between the client and the server, and sets a message receiving callback class for receiving the message message of the server;

[0022] The protocol definition module is used by the server to obtain the message content to be transmitted, customize a message sending list, a message header, a protocol field, a field type and a protocol type;

[0023] A message packet generation module is used for the server to assemble the message header, protocol field, field type, protocol type and message content into a message packet in TCP format based on the message sending list, and send the message packet to the client;

[0024] The message message sending and parsing module is used for the client to customize a message parsing list, receive the message message sent by the server through the message receiving callback class, and parse the message message through the message parsing list to obtain the message content to complete the communication.

[0025] Furthermore, the asynchronous connection module is specifically used for:

[0026] The client uses the TcpClient object to access the server IP and server port through asynchronous Socket programming to asynchronously connect to the server and set a message receiving callback class for receiving the message message from the server.

[0027] Further, in the protocol definition module, the message sending list is used to assemble the message message according to a preset format;

[0028] The message header at least includes a first start bit, a first end bit, a command code and a serial number;

[0029] The protocol field includes a special field and a regular field, which are represented by an enumeration type; the special field is used to set a custom identifier; the regular field includes at least a second start bit, a second end bit, a data type, a data field length, a data field, a check bit, a fixed value, a field content, a field value type, and a field length;

[0030] The field type is the type of the protocol field, which is represented by a data type.

[0031] Furthermore, the message generation module is specifically used for:

[0032] The server encodes the message content into encoded data based on the protocol type, assembles the message header, protocol field, field type, protocol type and encoded data into a byte stream based on the message sending list, generates a message packet in TCP format based on the byte stream and sends it to the client.

[0033] Furthermore, the message packet sending and parsing module is specifically used for:

[0034] The client customizes a message parsing list for storing the mapping relationship between protocol types and decoding methods, receives the message message sent by the server in real time through the message receiving callback class, parses the message message to obtain a byte stream including a message header, protocol field, field type, protocol type and encoded data, matches the corresponding decoding method from the message parsing list based on the protocol type, decodes the encoded data through the decoding method to obtain the message content, so as to complete the communication.

[0035] The advantages of the present invention are:

[0036] The client and the server are connected asynchronously, and a receiving message callback class is set up to receive the message message from the server; the server obtains the message content to be transmitted, customizes the message sending list, message header, protocol field, field type and protocol type, assembles the message header, protocol field, field type, protocol type and message content into a message message in TCP format based on the message sending list, and sends the message message to the client; the client receives the message message sent by the server through the receiving message callback class, and parses the message message through the customized message parsing list to obtain the message content to complete the communication; that is, before communication, the server encapsulates the message content to be transmitted based on the customized message sending list, and in actual application, the corresponding protocol field, field type and protocol type can be set as needed to meet personalized needs. The client parses the encapsulated message message based on the message parsing list that stores the mapping relationship between the protocol type and the decoding method, without the need to redevelop the corresponding communication protocol, saving the development time of the communication protocol, and ultimately greatly improving the scalability, flexibility and efficiency of communication. BRIEF DESCRIPTION OF THE DRAWINGS

[0037] The present invention will be further described below in conjunction with embodiments with reference to the accompanying drawings.

[0038] Figure 1 The present invention is a flowchart of a TCP-based custom protocol communication method.

[0039] Figure 2 It is a structural schematic diagram of a TCP-based custom protocol communication system of the present invention. DETAILED DESCRIPTION

[0040] The technical solution in the embodiments of the present application has the following overall idea: before communication, the server encapsulates the message content to be transmitted based on a customized message sending list. In actual application, the corresponding protocol fields, field types and protocol types can be set as needed to meet personalized needs. The client parses the encapsulated message message based on a message parsing list that stores the mapping relationship between protocol types and decoding methods. There is no need to redevelop the corresponding communication protocol, which saves the development time of the communication protocol and improves the scalability, flexibility and efficiency of communication.

[0041] Please refer to Figure 1 to Figure 2 As shown, a preferred embodiment of a TCP-based custom protocol communication method of the present invention comprises the following steps:

[0042] Step S1, the client is asynchronously connected to the server, and a message receiving callback class is set for receiving a message message from the server;

[0043] Step S2: The server obtains the content of the message to be transmitted, and customizes a message sending list, a message header, a protocol field, a field type, and a protocol type;

[0044] Step S3: The server assembles the message header, protocol field, field type, protocol type and message content into a message message in TCP format based on the message sending list, and sends the message message to the client;

[0045] Step S4: The client customizes a message parsing list, receives the message message sent by the server through the message receiving callback class, and parses the message message through the message parsing list to obtain the message content to complete the communication.

[0046] The present invention can be flexibly designed according to specific application requirements, only includes required functions, designs protocols that are more suitable for specific scenarios, implements customized data exchange mechanisms, avoids unnecessary complexity, has flexible configuration, and is convenient for modification when protocols are changed; and through a carefully designed custom protocol, the size of communication data packets (messages) and the number of communications can be reduced, thereby improving communication efficiency and reducing communication delays; it can also be expanded as needed to add new functions and features without being limited by the limitations of standard protocols, and can better adapt to specific network environments and data transmission requirements, thereby improving system stability and reliability.

[0047] The step S1 is specifically as follows:

[0048] The client uses TcpClient object to access the server IP and server port through asynchronous Socket programming to connect to the server asynchronously, and sets a receive message callback class for receiving the message message from the server. The receive message callback class is used to asynchronously receive the message message sent by the server from the network flow, and then trigger the corresponding event.

[0049] In the step S2, the message sending list is used to assemble the message message according to a preset format;

[0050] The message header at least includes a first start bit, a first end bit, a command code and a serial number;

[0051] The protocol field includes special fields and regular fields, which are represented by enumerated types; the special fields are used to set custom identifiers and can be freely expanded, such as command codes, serial numbers, unique identifiers, etc.; the regular fields at least include the second start bit, the second end bit, the data type, the data field length, the data field, the check bit, the fixed value, the field content, the field value type, and the field length; the check bit is obtained by performing a hash calculation on the message message, and the message message can be subsequently checked for integrity based on the check bit;

[0052] The field type is the type of the protocol field, which is represented by a data type;

[0053] The protocol types at least include authentication, heartbeat, data transmission and process control; in specific implementation, corresponding business instructions, interaction mechanisms and triggering opportunities can be set for each protocol type.

[0054] The step S3 is specifically as follows:

[0055] The server encodes the message content into encoded data based on the protocol type, assembles the message header, protocol field, field type, protocol type and encoded data into a byte stream based on the message sending list, generates a message message in TCP format based on the byte stream and sends it to the client through asynchronous Socket programming. In specific implementation, the key data in the message message can be encrypted to ensure the security of the message message transmission, for example, the encoded data is encrypted by the SM4 algorithm.

[0056] The step S4 is specifically as follows:

[0057] The client customizes a message parsing list for storing the mapping relationship between protocol types and decoding methods, receives the message message sent by the server in real time through the message receiving callback class, parses the message message to obtain a byte stream including a message header, protocol field, field type, protocol type, and encoded data, matches the corresponding decoding method from the message parsing list based on the protocol type, and decodes the encoded data through the decoding method to obtain the message content to complete the communication. The message parsing list is a mapping table. In specific implementation, the exception class can be customized to trigger and handle exceptions in specific situations, such as message format errors, unsupported protocol types, etc.

[0058] A preferred embodiment of a TCP-based custom protocol communication system of the present invention includes the following modules:

[0059] The asynchronous connection module is used for asynchronous connection between the client and the server, and sets a message receiving callback class for receiving the message message of the server;

[0060] The protocol definition module is used by the server to obtain the message content to be transmitted, customize a message sending list, a message header, a protocol field, a field type and a protocol type;

[0061] A message packet generation module is used for the server to assemble the message header, protocol field, field type, protocol type and message content into a message packet in TCP format based on the message sending list, and send the message packet to the client;

[0062] The message message sending and parsing module is used for the client to customize a message parsing list, receive the message message sent by the server through the message receiving callback class, and parse the message message through the message parsing list to obtain the message content to complete the communication.

[0063] The present invention can be flexibly designed according to specific application requirements, only includes required functions, designs protocols that are more suitable for specific scenarios, implements customized data exchange mechanisms, avoids unnecessary complexity, has flexible configuration, and is convenient for modification when protocols are changed; and through a carefully designed custom protocol, the size of communication data packets (messages) and the number of communications can be reduced, thereby improving communication efficiency and reducing communication delays; it can also be expanded as needed to add new functions and features without being limited by the limitations of standard protocols, and can better adapt to specific network environments and data transmission requirements, thereby improving system stability and reliability.

[0064] The asynchronous connection module is specifically used for:

[0065] The client uses TcpClient object to access the server IP and server port through asynchronous Socket programming to connect to the server asynchronously, and sets a receive message callback class for receiving the message message from the server. The receive message callback class is used to asynchronously receive the message message sent by the server from the network flow, and then trigger the corresponding event.

[0066] In the protocol definition module, the message sending list is used to assemble message packets according to a preset format;

[0067] The message header at least includes a first start bit, a first end bit, a command code and a serial number;

[0068] The protocol field includes special fields and regular fields, which are represented by enumerated types; the special fields are used to set custom identifiers and can be freely expanded, such as command codes, serial numbers, unique identifiers, etc.; the regular fields at least include the second start bit, the second end bit, the data type, the data field length, the data field, the check bit, the fixed value, the field content, the field value type, and the field length; the check bit is obtained by performing a hash calculation on the message message, and the message message can be subsequently checked for integrity based on the check bit;

[0069] The field type is the type of the protocol field, which is represented by a data type;

[0070] The protocol types at least include authentication, heartbeat, data transmission and process control; in specific implementation, corresponding business instructions, interaction mechanisms and triggering opportunities can be set for each protocol type.

[0071] The message generation module is specifically used for:

[0072] The server encodes the message content into encoded data based on the protocol type, assembles the message header, protocol field, field type, protocol type and encoded data into a byte stream based on the message sending list, generates a message message in TCP format based on the byte stream and sends it to the client through asynchronous Socket programming. In specific implementation, the key data in the message message can be encrypted to ensure the security of the message message transmission, for example, the encoded data is encrypted by the SM4 algorithm.

[0073] The message packet sending and parsing module is specifically used for:

[0074] The client customizes a message parsing list for storing the mapping relationship between protocol types and decoding methods, receives the message message sent by the server in real time through the message receiving callback class, parses the message message to obtain a byte stream including a message header, protocol field, field type, protocol type, and encoded data, matches the corresponding decoding method from the message parsing list based on the protocol type, and decodes the encoded data through the decoding method to obtain the message content to complete the communication. The message parsing list is a mapping table. In specific implementation, the exception class can be customized to trigger and handle exceptions in specific situations, such as message format errors, unsupported protocol types, etc.

[0075] In summary, the advantages of the present invention are:

[0076] The client and the server are connected asynchronously, and a receiving message callback class is set up to receive the message message from the server; the server obtains the message content to be transmitted, customizes the message sending list, message header, protocol field, field type and protocol type, assembles the message header, protocol field, field type, protocol type and message content into a message message in TCP format based on the message sending list, and sends the message message to the client; the client receives the message message sent by the server through the receiving message callback class, and parses the message message through the customized message parsing list to obtain the message content to complete the communication; that is, before communication, the server encapsulates the message content to be transmitted based on the customized message sending list, and in actual application, the corresponding protocol field, field type and protocol type can be set as needed to meet personalized needs. The client parses the encapsulated message message based on the message parsing list that stores the mapping relationship between the protocol type and the decoding method, without the need to redevelop the corresponding communication protocol, saving the development time of the communication protocol, and ultimately greatly improving the scalability, flexibility and efficiency of communication.

[0077] Although the specific implementation modes of the present invention are described above, those skilled in the art should understand that the specific implementation modes described are only illustrative and are not intended to limit the scope of the present invention. Equivalent modifications and changes made by those skilled in the art in accordance with the spirit of the present invention should be included in the scope of protection of the claims of the present invention.

Claims

1. A TCP-based custom protocol communication method, characterized in that: The steps include: Step S1, the client is asynchronously connected to the server, and a message receiving callback class is set for receiving a message message from the server; Step S2: The server obtains the content of the message to be transmitted, and customizes a message sending list, a message header, a protocol field, a field type, and a protocol type; Step S3: The server assembles the message header, protocol field, field type, protocol type and message content into a message message in TCP format based on the message sending list, and sends the message message to the client; Step S4: The client customizes a message parsing list, receives the message message sent by the server through the message receiving callback class, and parses the message message through the message parsing list to obtain the message content to complete the communication.

2. A TCP-based custom protocol communication method as claimed in claim 1, characterized in that: The step S1 is specifically as follows: The client uses the TcpClient object to access the server IP and server port through asynchronous Socket programming to asynchronously connect to the server and set a receive message callback class for receiving the message message from the server.

3. The TCP-based custom protocol communication method according to claim 1, characterized in that: In step S2, the message sending list is used to assemble a message message according to a preset format; The message header at least includes a first start bit, a first end bit, a command code and a serial number; The protocol field includes a special field and a regular field, which are represented by an enumeration type; the special field is used to set a custom identifier; the regular field includes at least a second start bit, a second end bit, a data type, a data field length, a data field, a check bit, a fixed value, a field content, a field value type, and a field length; The field type is the type of the protocol field, which is represented by a data type.

4. The TCP-based custom protocol communication method according to claim 1, characterized in that: The step S3 is specifically as follows: The server encodes the message content into encoded data based on the protocol type, assembles the message header, protocol field, field type, protocol type and encoded data into a byte stream based on the message sending list, generates a message packet in TCP format based on the byte stream and sends it to the client.

5. The TCP-based custom protocol communication method according to claim 1, characterized in that: The step S4 is specifically as follows: The client customizes a message parsing list for storing the mapping relationship between protocol types and decoding methods, receives the message message sent by the server in real time through the message receiving callback class, parses the message message to obtain a byte stream including a message header, protocol field, field type, protocol type and encoded data, matches the corresponding decoding method from the message parsing list based on the protocol type, decodes the encoded data through the decoding method to obtain the message content, so as to complete the communication.

6. A TCP-based custom protocol communication system, characterized in that: Includes the following modules: The asynchronous connection module is used for asynchronous connection between the client and the server, and sets a message receiving callback class for receiving the message message of the server; The protocol definition module is used by the server to obtain the message content to be transmitted, and customize a message sending list, a message header, a protocol field, a field type, and a protocol type; A message packet generation module is used for the server to assemble the message header, protocol field, field type, protocol type and message content into a message packet in TCP format based on the message sending list, and send the message packet to the client; The message message sending and parsing module is used for the client to customize a message parsing list, receive the message message sent by the server through the message receiving callback class, and parse the message message through the message parsing list to obtain the message content to complete the communication.

7. A TCP-based custom protocol communication system as claimed in claim 6, characterized in that: The asynchronous connection module is specifically used for: The client uses the TcpClient object to access the server IP and server port through asynchronous Socket programming to asynchronously connect to the server and set a receive message callback class for receiving the message message from the server.

8. A TCP-based custom protocol communication system as claimed in claim 6, characterized in that: In the protocol definition module, the message sending list is used to assemble message packets according to a preset format; The message header at least includes a first start bit, a first end bit, a command code and a serial number; The protocol field includes a special field and a regular field, which are represented by an enumeration type; the special field is used to set a custom identifier; the regular field includes at least a second start bit, a second end bit, a data type, a data field length, a data field, a check bit, a fixed value, a field content, a field value type, and a field length; The field type is the type of the protocol field, which is represented by a data type.

9. A TCP-based custom protocol communication system as claimed in claim 6, characterized in that: The message generation module is specifically used for: The server encodes the message content into encoded data based on the protocol type, assembles the message header, protocol field, field type, protocol type and encoded data into a byte stream based on the message sending list, generates a message packet in TCP format based on the byte stream and sends it to the client.

10. A TCP-based custom protocol communication system as claimed in claim 6, characterized in that: The message packet sending and parsing module is specifically used for: The client customizes a message parsing list for storing the mapping relationship between protocol types and decoding methods, receives the message message sent by the server in real time through the message receiving callback class, parses the message message to obtain a byte stream including a message header, protocol field, field type, protocol type and encoded data, matches the corresponding decoding method from the message parsing list based on the protocol type, decodes the encoded data through the decoding method to obtain the message content, so as to complete the communication.