22

I was searching a lot but I could not find any resources which go through the building of an own protocol which uses TCP as a transport layer. What are the necessary steps? The protocol should be some kind of "Control protocol" for devices. So I can send commands to devices and control them and get information back.

So how would one implement a custom protocol? Is it all about defininig what commands can be sent and how the receiver reacts to different commands? Say I am defining some custom commands with xml send them over the wire/air, using tcp, and have some logic there which reacts to the sent command and replies. Is this a way one could implement a "protocol" ? Is this even called a "protocol"?

kind regards.

1
  • 1
    Yes, that's a "protocol". Take a look at the original documentation for some of the classic TCP/IP protocols for examples. RFC 821 for SMTP is a good example. Aug 15, 2013 at 11:07

1 Answer 1

26

As long as you can write a specification that defines the data you send through the TCP socket, you've got your own protocol.

It's mostly about defining commands and payloads. You've got to serialize your command packet before putting them through TCP. Endianness is a common pitfall if you pack the packet in binary format. XML and JSON are common text-based data exchange formats. Personally I'm pro-JSON.

Do refer to BSON, MessagePack or protobuf for binary serialization. They pack the typed data into binary so they are usually more performant, more compact in size and have better type checking than text based serialization. They also handle endian conversion, packet versioning and provide drivers/bindings in various languages. The server and client could have been written in different languages.

EDIT: added RFC samples

Seeing the comment by Ross Patterson, I also recommend reading RFC for protocol definition references. RTSP and HTTP are text protocols, RTP and media formats (MPEG4 AV, H-264) are binary protocols.

EDIT:

Demystifying Protocols and Serialization Performance with Todd Montgomery
What is a Protocol? (Deepdive) - YouTube

2
  • Serialization is one thing, but what about framing of messages? I know protobuf is not self delimiting, so you need to send length + data. When using JSON, do you usually prepend your JSON payload with length ?
    – Droopycom
    Apr 19, 2019 at 22:35
  • I haven't tried sending raw protobuf data, I used gRPC which uses HTTP2 to specify the content length. When you define the message format as { length, data } you're indeed defining your protocol, which by definition is how the data is frames, and serialization is a prerequisite.
    – leesei
    Apr 21, 2019 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.