Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the methods of socket packet serialization and deserialization

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are socket packet serialization and deserialization methods". In daily operations, I believe many people have doubts about what socket packet serialization and deserialization methods are. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer your doubts about what socket packet serialization and deserialization methods are. Next, please follow the editor to study!

1. Background of this article

Often do Cpact S, client and server communication is basically TCP/UDP communication, sockets use fly.

For example, we have a system, which is divided into several system sub-module processes:

C++ server

Android client

IOS client

WPF desktop management side.

Several modules communicate with each other through TCP or UDP. Packet parsing and assembly are routine operations. We define the packet format as follows:

A packet contains a header and a packet body, which are defined as follows:

Packet header serial number field name data type Note 1 message identification int is used to identify whether the packet is legal 2 name string current message name, used to identify packet type 3 version number int current message version number, allowing multiple versions of messages in the program for version iteration

Contains these three fields: message ID, name, version number, which uniquely identifies the message object.

Packet body serial number field name data type remarks 1 field 1 data type field 12 field 2 data type field 2

The package body defines the field information directly, just as it defines the class properties.

The data types in the header and body of the packet are defined as follows:

Packet field type definition ordinal data type remarks 1int4 byte integer value 2string format: string actual value byte length + string actual value byte3char byte value 4 list format: 4 byte list length + list actual data value byte5 dictionary as above, specific source code

Other data types are similar, with complex data types using a value byte length of 4 bytes + the actual value byte.

Give a test packet serial number field name data type Note 1 message ID int value: 0x4A5346042 message name string three countries information, value: "ThreeCountries" 3 version number int value: 14 number int give the three countries a number Value: 15 countries name string value: "Shu" 6 emperor string value: "Liu Bei" 7 generals int58 number 1 int value: 19 general 1 name string value: "Zhang Fei" 10 general 1 remarks string value: "three axes" 11 general 2 number int value: 212general 2 name string value: "Guan Yu" 13 general 2 remarks string value: "Qinglong Yanyue knife" 14 general 3 number int value: 315 general 3 names string value: "Zhao Yun" 16 general 3 remarks string value: "very fierce" 17 general 4 number int value: 418 general 4 name string value: "Ma Chao" 19 general 4 remarks string value: "strong" 20 general 5 number int value: 521 general 5 name string value: "Huang Zhong" 22 general 5 remarks string value: "old and strong"

To get a general understanding:

The first three fields are the packet body: used to identify the entire packet and facilitate packet parsing

The package at the back is simply a brief introduction to the country information of the three Kingdoms. The first three fields are the basic information of one of the three Kingdoms: number, name, emperor, followed by a list of information about the country's senior generals, each with a number, name, remarks, and so on.

Define data object

Based on the packet definition, we can quickly define classes for use, whether you are C++ or Java. Here is the corresponding class I wrote in C # for serialization and deserialization:

/ three Kingdoms / public class ThreeCountries {/ get or set ID / public int ID {get; set;} / get or set the country name / public string Name {get; set;} / get or set the emperor / / public string Emperor {get; set } / get or set the list of selected courses / public List Courses {get; set;} public override string ToString () {return $"one of the three Kingdoms {ID}: {Name} Emperor {Emperor}, there are {Courses.Count} famous generals" }} / three Kingdoms General / public class FamousGeneral {/ get or set the number / / public int ID {get; set;} / get or set the name / public string Name {get; set } / get or set the description / public string Memo {get; set;} public override string ToString () {return $"{ID}: {Name} = > {Memo}";}}

How do you serialize and deserialize the packets given above? The conversion to data is as follows, which is discussed in the next section.

ThreeCountries shuKingdom = new ThreeCountries {ID=1,Name= "Shu", Emperor = "Liu Bei", Courses = new System.Collections.Generic.List {new FamousGeneral {ID=1,Name= "Zhang Fei", Memo= "three axes"}, new FamousGeneral {ID=2,Name= "Guan Yu", Memo= "Qinglong Yanyue Dao"}, new FamousGeneral {ID=3,Name= "Zhao Yun", Memo= "very fierce"}, new FamousGeneral {ID=3,Name= "Ma Chao" Memo= "strong"}, new FamousGeneral {ID=3,Name= "Huang Zhong", Memo= "old and strong"},}} two。 General operation serialization

The code is too cumbersome, so I'll just write an irregular pseudo code.

Define a byte array First, write packet header 1, write 4-byte message identification: 0x4A534604 calculates the length of the message object name string "ThreeCountries", and converts the string to byte array 2, writes 2-byte bytes array length, writes the actual byte array value 3, writes 4-byte message version number 2, writes 4-byte message body 4, writes 4-byte general number cycle each general information Write 5 in turn, write general 1 no. 6, write 1 general 7, write grand prize 1 note 8, write grand general 2 no. 9, write 3 general 10, write grand prize 4 remarks. Write vomit, ellipsis deserialization do not want to write, tired of routine operation

Define a serialization interface in which each network object implements the serialization and deserialization interface

Public interface ISerializeInterface {byte [] Serialize (T t); T Deserialize (byte [] arr);} public class ThreeCountries: ISerializeInterface {public byte [] Serialize (T t) {/ / write the above serialization code here} T Deserialize (byte [] arr) {/ / write the above deserialization code here, sorry I didn't write}} 3. Operation of this article

After writing Demo for a long time, the article may be a little too bad. I guess the reader will not look at the code carefully and go directly to the Github check project, .

Let me say briefly, the implementation is very simple, define some features, the code file in the red box below:

It is easy to use. Add features to the above data class without much change. Take a look at the following code:

/ [NetObject (Name = "ThreeCountries", Version = 1)] public class ThreeCountries {/ get or set ID / / [NetObjectProperty (ID = 1)] public int ID {get; set;} / get or set the country name / / [NetObjectProperty (ID = 2)] public string Name {get; set } / get or set Emperor / / [NetObjectProperty (ID = 3)] public string Emperor {get; set;} / get or set the list of selected courses / [NetObjectProperty (ID = 4)] public List Courses {get; set;} public static NetObjectAttribute CurrentObject = null Static ThreeCountries () {CurrentObject = NetObjectSerializeHelper.GetAttribute (default (ThreeCountries));} public override string ToString () {return $"one of the three Kingdoms {ID}: {Name} Emperor {Emperor}, there are {Courses.Count} famous generals" }} / three Kingdoms General / public class FamousGeneral {/ get or set the number / [NetObjectProperty (ID = 1)] public int ID {get; set;} / set the name / [NetObjectProperty (ID = 2)] public string Name {get; set } / get or set description / [NetObjectProperty (ID = 3)] public string Memo {get; set;} public override string ToString () {return $"{ID}: {Name} = > {Memo}";}}

If you look carefully, only the NetObject attribute is added to the outer class (ThreeCountries), and the NetObjectProperty attribute is added to the attribute to identify the message name, version number, and the serialization and deserialization order of each attribute, respectively, and the sub-object Courses attribute used in the class only needs to add the attribute attribute, as above.

Add the unit test below, and the test passes:

At this point, the study on "what are the methods of socket packet serialization and deserialization" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development