Reputation: 2532
I have several clients, who writes to one socket (not port), when they write together, I receive the garbage, all data from all clients are merged.
all clients are in the same program in Threads.
I need to lock write()
ASocket.Connection.Socket.LOCK; // need to be thread safe
ASocket.Connection.Socket.Write(buf);
ASocket.Connection.Socket.UNLOCK; // need to be thread safe
How can I do it ?
Thanks.
Delphi 2010, Indy 10, Win7
Upvotes: 3
Views: 1047
Reputation: 28688
You can use TCriticalSection
(SyncObjs
unit): put the Write
between Enter
and Leave
:
CriticalSection.Enter;
try
ASocket.Connection.Socket.Write(buf);
finally
CriticalSection.Leave;
end;
The methods Acquire
and Release
do the same (doc). Important: if you write to the socket at multiple points of your code, you must use the same object (the one I called CriticalSection
in the above example).
Upvotes: 2