简体   繁体   English

如何将从TServerSocket发送流同步到Multi TClientSocket

[英]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 ...? 有没有一个解决方案可以在Delphi的ScktComp.dcu中使用TServerSocket,让我可以将单个文件流或多个文件流同步到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. 我需要的是用一个好的表达式替换循环函数,这个表达式允许我将任何文件流或任何文本或任何命令传输或发送到多TClientSocket但是使用Syncronize方法并且意味着与我的服务器连接的每个客户端都应该收到我的TClientServer同时发送或转移的内容,而不会丢失其他客户端或一个接一个地等待发送给其他人...最后,如果可以在这个组件上,我将感谢任何一个回复的建议,是非常感谢。

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. TCP根本没有向多个客户端广播数据的概念,因此您必须在代码中手动实现它。 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. 我喜欢使用TServerSocket.OnClientConnect事件为缓冲区创建TMemoryStream并将其分配给TCustomWinSocket.Data属性,以便可以轻松跟踪它。 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. 否则,尽可能多地将字节发送到客户端的套接字,如果发送失败并出现WSAEWOULDBLOCK错误,则将任何未发送的字节附加到缓冲区的末尾。 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. 使用TServerSocket.OnClientWrite事件完成从缓冲区发送任何未发送的数据,随时从缓冲区的前面删除发送的字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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