M
M
maaGames2019-12-24 14:39:17
Computer networks
maaGames, 2019-12-24 14:39:17

How to send many messages through Socket?

I use the Poco library ( pocoproject.org ) I transfer

data about the progress of the program to another program via sockets, there can be a lot of messages, but small ones (hundreds / thousands per second) and not very many large ones (hundreds of megabytes, but with an interval of several minutes ). Delays in sending and receiving messages are not important (as long as the order of their delivery does not change) and the sending and receiving applications can "hang" while receiving the message.

For sending/receiving, I use SocketOutputStream/SocketInputStream and write/read data with write/read functions. To avoid trying to write to the socket from different streams at the same time, the creation of the SocketOutputStream is surrounded by a Poco:Mutex. (also tried omp critical for experiment)

If messages are sent infrequently (for example, stepping in the debugger), then everything works correctly, regardless of the size of the transmitted data. In a real program, messages are sent very often and something gets corrupted. At the beginning of each block of data, I write a "magic number" to make sure it's the correct data. And it often turns out that the data in the SocketInputStream is incorrect. For each message, a new SocketOutputStream object is created, in theory, when the object is deleted, flush is done and the data should be sent immediately.

It all looks like a part of the data of the second block has been added to the data of the first block. For example, there were two objects of 10 bytes each, 15 bytes arrived, the first object counted its 10 bytes and the "tail" was removed. And the next time poll() was triggered, the remaining 5 bytes were counted and there was no "magic number" at their beginning, and everything was gone... This seems obvious, but I specifically use the SocketOutputStream classes to get rid of manual management of buffers, tails, etc. d. etc.

Actually, the question is:
How can Poco correctly transmit and receive large amounts of data of different sizes (sontimarrays per second, tens of bytes in size and (rarely) piece arrays of hundreds of megabytes in size)? Which can be sent asynchronously from multiple threads.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
tsarevfs, 2019-12-24
@maaGames

Not much can be said without code. If it is possible to upload to github, then there would be more chances. There are 3 suspicious points:
Is it necessary? Why not use this stream as long as the socket lives?
Does the write directly fall into the stream and flush (destructor) under this mutex?
As far as I could understand from the documentation, this class knows nothing about message boundaries. This is about the same stream as for writing to a file. Therefore, if the message size can be different, then you need to watch how you read the data.
For debugging network code, I can advise logging instead of a debugger.

R
res2001, 2019-12-24
@res2001

You seem to be using TCP for transmission. In TCP there is no division into messages, all data is transmitted in one stream in the order in which it was sent. Those. the situation is quite real when you send for example 2 messages of 10 bytes each, and read 3 times 5, 10, 5 bytes at a time. Thus, you must split into messages on the receiving side yourself.
ROSO has nothing to do with it - this is how the TRS protocol works. Instead of TCP, you can use UDP - here you will receive messages. But there is a difficulty with defragmentation - if the message size is larger than the MTU, then the message will be fragmented during transmission and it is not a fact that the pieces will reach and be collected. Well, UDP does not guarantee delivery, i.e. messages can be lost and neither the recipient nor the sender will know about it.
You can also use the SCTP protocol, it works with messages just like UDP, but it guarantees delivery. SCTP is part of the standard TCP/IP stack. Whether there is support for it in POCO, I do not know.

M
mayton2019, 2019-12-25
@mayton2019

The author is trying to build his application protocol on top of sockets. You don't need to do this because such protocols have already been created. Keywords: jms, mq, apache-mq, kafka, rabbitmq, ibmmq.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question