Network protocols of several C ++ applications

I am programming a Client / Server application for my C ++ project. The working group indicated or discussed the entire protocol of the application. The main idea is that we have 3 protocols.

  • Text protocol: send and receive some information in a string format between a client and a server.
  • Binary protocol: the client constantly sends some status data to the server.
  • Binary protocol: the client constantly transmits some data, such as sound / video / images / text

All protocols should work on different ports. I implemented Socket-Class, which is responsible for creating and listening to a socket, receiving a connection with a client. There is also a function for receiving / sending data based on strings and receiving / sending data based on binary files.

In the next step, I would like to define 3 classes. Each of them should be responsible for creating one socket in a new thread and is responsible for the protocol that was defined for this port (p. 1-3). Therefore, in the end I will get 3 sockets (1 socket for one port).

My question is: if I think in the right direction? Perhaps you can recommend me some design patterns for using different application protocols. It would be great if you could recommend me some projects or code that might be similar to my project.

Thanks.

+4
source share
2 answers

You must separate the socket class from the three protocol handlers - do not have methods for processing text and binary data on Socket, or you inadvertently encourage people to mix and match data types on the same socket, which is clearly not what you want.

Your socket should provide simple functions of connecting / disconnecting, transmitting and transmitting data, and decoding and encoding of the sent / received data is then performed in another object, probably selected from 3 new classes (one per protocol).

In general, I doubt the use of text data. This is inefficient compared to almost any serialization library that you could name. You can trade a little extra debugging for a lot of hard-written data analysis and error checking code and an accompanying malfunction in the processor cycle. If the text data is simple enough (not really structured, such as XML), then this causes fewer problems.

Protocol 2. can be implemented using UDP rather than TCP if status information is not critical. This is less than what you would need to do.

+2
source

You can use enet . It provides reliable and unreliable UDP communications and will do most of the hard work of communicating with you.

0
source

All Articles