简体   繁体   中英

how to syncronize sending stream from TServerSocket to Multi TClientSocket

Is there a solution to work with TServerSocket in ScktComp.dcu from delphi that let me sending with syncronize a single file stream or multi file stream to a Multi TClientSocket ...? I have this code here :

procedure TFrmMainServer.ServerSocket1ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
var 
   a, ACount: integer;
   MyText: String;
   MyBuffer: pchar;
   MyStream: TMemoryStream; //or TFileStream;
begin
...
...
...
  for a := 0 to ServerSocket1.Socket.ActiveConnections - 1 do
  begin
      ServerSocket1.Socket.Connections[a].SendText(MyText);
      // or 
      //ServerSocket1.Socket.Connections[a].SendStream(Mystream); // for files transfering ....
      // or
      //ServerSocket1.Socket.Connections[a].SendBuf(MyBuffer, ACount);
  end;
end;

What I need exactly is to replace the loop function with a good expression that let me transfer or send any file stream or any text or any command to a multi TClientSocket but with a Syncronize methode and meaning that every client that is connected with my server should recieve what my TClientServer is sending or transfering at the same time and without losing the others clients or waiting one by one to send to others ... Finally if is it possible on this component I will appreciate any suggestion from any one that reply and is well appreciated.

You cannot avoid the loop if you want to send the same piece of data to multiple clients. TCP simply has no concept of broadcasting data to multiple clients, so you have to implement it manually in code. That usually means making a separate copy of the data for each client so they can send the data in parallel.

There are two ways to handle this, and both require using a per-client outbound data buffer. I like to use the TServerSocket.OnClientConnect event to create a TMemoryStream for the buffer and assign it to the TCustomWinSocket.Data property so it can be tracked easily. Use whatever makes sense for your code.

  1. put the server into thread-blocking mode so that each client runs in its own dedicated worker thread. Wrap the data buffer with a critical section. When you want to write data to a particular client, lock the client's buffer, append the data to the end of the buffer, and unlock it. Let the client's worker thread continuously check the buffer for new data and send it over the client's socket. Remove sent bytes from the front of the buffer as you go.

  2. put the server into non-blocking mode so no worker threads or locks are used. When you want to write data to a particular client, check if the client's buffer already has any unsent bytes in it. If so, just append the new bytes to the end of the buffer and move on. Otherwise, send as many bytes as you can to the client's socket, and if the send fails with an WSAEWOULDBLOCK error then append any unsent bytes to the end of the buffer. Use the TServerSocket.OnClientWrite event to finish sending any unsent data from the buffer, removing sent bytes from the front of the buffer as you go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM