Delphi: sending data through multithreading of a single socket

Can someone give me an idea how to send and receive data through one connection in multi-threaded mode.

The model is as follows:

Model

I know that if all three clients send data at the same time, "client X" will receive a merge of all received data, and "client X" cannot separate this data to determine which part of which is the client.

Delphi 2010, Indy, Win7.

Sorry if my English is bad, I hope you understand the idea.

+4
source share
1 answer

You need to implement a locking mechanism, such as a critical section or a mutex, to prevent multiple threads from simultaneously writing to the socket.

When receiving data intended for several streams, it is necessary to read only in one stream and transfer data to other streams if necessary.

In any case, you need to photograph your data so that the recipient knows where one message ends and the next begins. Either send the message length before sending the contents of the message, or by sending a unique separator between messages that will never be displayed in the messages themselves.

+6
source

Source: https://habr.com/ru/post/1385459/


All Articles