I. INTRODUCTION and Agreement
previously developed a project-in-vehicle navigation system. The first problem encountered is how the hardware device communicates with the server.
The key is the communication protocol!
As we all know: in order to realize communication, communication agreement must be reached by both parties.
Words do not say much, and look at the agreement:
———————————————— Gorgeous split-line —————————————————
Do the above-mentioned protocols explain whether they look very big?
How can hardware devices that follow such communication protocols communicate successfully with servers and PCs?
and please crossing a little! And listen to my word!
two. Basic knowledge-tcp and sticky bag
As we all know, the core of the Internet is the TCP/IP protocol cluster. TCP (Transmission Control Protocol) is a connection-oriented, reliable, byte-stream-based transport layer protocol. In addition we have probably heard a word, called "sticky bag", but many people unclear its connotation. In fact, the sticky packet problem and TCP are closely related. Because TCP is connection-oriented and byte-stream-based, we can use a pipe to describe how TCP works.
The byte stream is the same as the current, and when two messages are read together, you cannot separate the boundaries of the two.
three. Solution of sticky packet-message delimitation
In order to solve the problem of sticky packets, the message must be bound.
Method 1: Text protocol mode
Method 2: Binary protocol mode
The text protocol pattern is the basis of dividing the message by adding special flags at the end of the message; The binary protocol encapsulates the message as a message header + message body, resolves the problem of the message body by parsing the fixed-length message header and then getting the message body length from the message header.
Four. Return to question-Agreement selection
The above is the message structure of the hardware device, from which we can see both the shadow of the text protocol and the shadow of the binary protocol.
Because of the flags, the text protocol can be used.
then we will start the flag bit as part of the message header, the rest of the part as the message body, then is a binary protocol form. The two requirements for the binary protocol are: 1. message header length 2. Message body lengths can be resolved in the message header. And this message structure satisfies this requirement.
Five. Examples of text protocol implementations
//Summary://The Text Protocol helper interface. Public InterfaceItextcontracthelper {//Summary://a collection of Message end identifiers (byte arrays that are encoded as a result). //For example, the general application is to use "\" as the message end flag, then there is only one element in the collection ("\" binary). //Some applications may have multiple identifiers (such as "\", "\ n", and others) that can be used as the end flag for a message, and there are multiple elements in the collection. //if set to NULL, the engine does not identify and construct message integrity, and each time it receives data, it directly triggers the messagereceived event. list<byte[]> Endtokens {Get; } }
The Text Protocol Helper interface defines the most basic specification for a text protocol-with a message terminator. Next, let's look at a simple implementation of the interface.
Public classDefaulttextcontracthelper:itextcontracthelper { PublicDefaulttextcontracthelper (params string[] endtokenstrs) { This. Endtokens =Newlist<byte[]>(); ; if(Endtokenstrs = =NULL|| Endtokenstrs.length = =0) { return; } foreach(stringStrinchendtokenstrs) { This. Endtokens.add (System.Text.Encoding.UTF8.GetBytes (str)); } } Privatelist<byte[]>Endtokens; Publiclist<byte[]>Endtokens {Get { return This. Endtokens; } } }
In fact, the essence of the text protocol is: The Terminator of the message after encoding (such as UTF-8) the resulting byte array as a character stream to identify the message boundary flag.
We inject it into the communications engine, and the engine splits the message according to the flags we set.
// Initialize and start the client engine (TCP, text protocol) this. Tcppassiveengine = Networkenginefactory.createtexttcppassiveengine (thisint. Parse (Thisnewdefaulttextcontracthelper (")");
The demo uses "\" as the message end flag. We know that the message terminator is that we artificially add the message to the tail, and the real message is that it doesn't have that content. So when we get the message we need to remove the Terminator, which is the solution.
void byte [] bmsg) { string// message using UTF-8 encoded msg = Msg. Substring (01// remove this from the end tag " \" .) ShowMessage (msg);
}
attached: Text Protocol demo source download
Six. Binary Protocol demoimplementation Examples
//Summary://Binary Protocol Helper interface. Public InterfaceIstreamcontracthelper {//Summary://the length of the message header. intMessageheaderlength {Get; } //Summary://The length of the message body is resolved from the message header (note that it is not the length of the entire message, but the length of the body that does not contain the message header). // //Parameters://Head://complete message header, fixed length of messageheaderlength intParsemessagebodylength (byte[] head); }
public class Streamcontracthelper:istreamcontracthelper { public int Messageheaderlength { get {return 8 ; }} public int parsemessage Bodylength (byte [] head) {
return Bitconverter.toint32 (head,
0
); } }
We inject it into the communication engine, the engine can identify the message header, remove the message body length to split a message.
// Initialize and start the client engine (TCP, text protocol)
this. Tcppassiveengine = Networkenginefactory.createstreamtcppassivengine (thisint. Parse (Thisnewstreamcontracthelper ());
attached: Binary Protocol demo source Download
Seven. Summary
This paper aims to introduce the general method of text protocol design, through the understanding of the essence of text protocol and binary protocol, we can realize its communication protocol according to the actual need. You can do this in accordance with this general method of the implementation of the first I gave the satellite positioning System communication protocol.
There are many applications that need to communicate with hardware devices, such as communication with GPS devices, communication with some SCM devices in the factory floor, communication with IoT devices, etc., as long as they have mastered the communication protocol of these devices, or adopt the text protocol or adopt binary protocol, the communication problem will be solved!
Finally, with the increasingly mature and popularization of the HTML5 WebSocket technology, the application of B/s architecture to the application of C/s architecture is gradually moving toward unification. Interested friends can refer to: Get through b/S and c/s! Let HTML5 websocket and. NET sockets share a service side!
Development of the Department of Transportation • Vehicle navigation system in the first level--hardware terminal how to communicate with the server--Play the Communication protocol (source download)