Home > other >  About netty encapsulation in baotou
About netty encapsulation in baotou

Time:10-02

Baotou network data structure:
# define FLAG 0 x55aa55aa//network data in baotou sign
# define DWORD unsigned int

Typedef struct _TAG_DetectNetHead_T
{
DWORD nFlag;//network packet marking (0 x55aa55aa)
DWORD nDataLen;//data length, not including the length of the baotou's own
DWORD nCommType;//type 0: key staff query request; 1: the heart; 2: identity information
} TAG_DetectNetHead_T;



In the Java project with netty how to encapsulate the baotou, and how to solve? To use binary? What is your specific?

CodePudding user response:

Int nFlag=0 x55aa55aa;
//int nDataLen=message body bytes
Int nCommType=0;//type 0: key staff query request; 1: the heart;
These three fields are header, pushed to the side, the side to get answers from 12 bytes, as message interception header information (int four bytes);
NDataLen decide the length of the message body, will know that the length of the message to end, then according to the length of the interception of corresponding to the number of bytes, how the message body constraints, then go to the agreement;
Specific how to send, I probably write:
ByteBuf encoded=CTX. Alloc (.) buffer (12 + Data. Length);//data is the message body length
Byte [] nFlag_b=new byte [4].
//put int into an array of bytes nFlag_b, then into ByteBuf, each type of converted to the corresponding byte array, your turn, also can be encapsulated into a class, more specification,
Encoded. WriteBytes (nFlag);
//... All together to send to the end, I write here is that the server code, the client the same,
CTX. WriteAndFlush (encoded);



  • Related