About Socket Communications When developing communication protocol

          In the system development process, if it comes to a Socket Communications, it would have to develop protocols good communication, and only developed a good rule, behind the work was to facilitate a smooth start, the production of communication protocols to take into account the security of communications, information necessary to be encrypted, but also taking into account the scalability of the system, if you later need to add a command or modify the command to ensure that the work can be easily performed.

          The agreement include the following:

First, the system default convention

1. String This protocol used, such as a password, a median of six.

2. Communication: TCP, UDP. This Agreement is the default TCP / IP mode.

3. Line detection packet, packet synchronization, mainly used in a TCP / IP connection mode longer request initiated by the terminal (WinForm), commands reserved for the server.

Real-time information packet, mainly used in TCP / IP mode short connection request initiated by the terminal (Webform), the relay station requests the server for the terminal winform, then came command feedback.

As shown below:

A command control flow

         Webform terminal requests must first confirm Winform terminal is connected to the intermediate server (the Socket Server) prior to real-time information packet, must first terminal connected Remoting Server before Winform synchronization information, so Winform terminal periodically sends connection confirmation data to the Server

End, to confirm the connection status.

 

And a terminal into a terminal 1. webform winform terminal.

2. Winform terminal timing (3min) transmits an acknowledgment packet to the online Server, Server Winform feedback data packets to the terminal, if the received feedback data packet timeout (10s), then the re-sending, receiving five consecutive feedback timeout, was offline, Server side need to reconnect.

Two , the communication protocol command

Datagram Protocol format

 

         Data packet: the packet header length + number + terminal command word + + + Total number of frames of the content of several frames + + + status check frame end +

       Description:

              Each message is beginning to 0x00, 0xFF to end.

Each message can only send a command word, the maximum length of each message is 1024 (including 1024 bytes), the portion of the batch transmission over 1024, this cycle.

When reading data, the contents of the default is 0x00.

Terminal number to "K0000001" exemplified

 

 

name

Header

Packet content

End of Packet

meaning

News length

Header

Terminal Number

7

The total number of frames

The first few frames

content

check

status

End of frame

position

0

1

2

1 byte

4

5

6

7

8

9

length

2 bytes

1 byte

8 bytes

0xFF

2 bytes

2 bytes

N bytes

1 byte

1 byte

1 byte

content

0x0000

0x00

K0000001

0x00

0x0000

0x0000

….

0xFF

0x00

0xFF

Three , the command word down is to develop protocols:

Develop specific meaning of each word of command, what kind of content delivery. How to determine the correctness of the transmission of content,

Identification of the authenticity of content delivery, defined status code. Under brief: by sending a status code to determine the

State data packets, successful execution of the command. Be judged by the content of the parity bit is true and reliable.

By encryption and decryption algorithms ensure the safety of the content. It requires encrypted content generally includes the terminal number, the transmitting

Content.

 

 

 

Reproduced in: https: //www.cnblogs.com/kevinGao/archive/2011/11/07/2238880.html

Guess you like

Origin blog.csdn.net/weixin_34324081/article/details/93053022