Good day.

I use the net library to communicate with the server. The client is written in C ++ and uses normal system sockets. I transfer to the client a set of data, in the form of portions. I noticed that the server does not wait until the client receives the data and immediately sends the next packet. Because of this, the client does not process the intended portion, but rather joins some portions together. Before me was the question: How to ensure that the server waited for the moment - when the packet reaches the client, and only then send the next packet. I read a little about the specifics of sockets, I learned that sockets can be blocked and not blocked. I mean, at the native level, there is something like the TCP_NO_WAIT flag, which says that you do not need to wait for a response about the delivery of data, which is why, without waiting for it, it immediately sends the next batch.

So, how to make a socket blocked? So that he waited for a response on the delivery and only then sent the next packet.

  • You will have to divide the "portions" yourself so that you do not configure it on the server; on the way, a node can glue or break up your pieces. - Vladimir Gamalyan
  • From the solutions, where there will be ready "portions", you can look at the websockets. - Vladimir Gamalyan
  • They say that in JS the word "blocking" is a terrible curse :) - D-side
  • Socket blocking is absolutely not about business. As well as transferring a client to web-sites absolutely nothing. According to the websocket protocol, the data still comes in frames. The only thing is that they are already labeled and their initial, current and final frames are visible, which allows you to collect the desired 'portion'. And transport in any case is the TCP protocol, which fragments your “portions”. Therefore, in any case, you need to independently control this process. Which way to choose depends on the format of the data sent. - Max ZS
  • I need to transfer pictures of different formats, this is the task. - Tzendos

1 answer 1

No The TCP protocol sends a continuous stream of data, without partitioning. You need to think of a way to break the stream into messages yourself.

Usually, in such cases, either a special separator between the messages is made (a null character or a line feed) —or else, the length of the message is recorded before the message. The second method is more universal.