IdTCPServer怎么样主动跟指定客服端发送数据?

tjs_125 2013-08-20 06:54:15
如题,IdTCPServer怎么样主动向指定客服端发送数据?
如果是收到客服端的信息,需要发送回复信息,我知道在OnExecute事件里面就可以发送了。但主动向客服端发送信息我就不会了。
...全文
347 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wylton 2013-08-22
  • 打赏
  • 举报
回复
List := tcpServer.Threads.LockList; try for Count := 0 to List.Count -1 do try TIdPeerThread(List.Items[Count]).Connection.WriteLn(Msg); except TIdPeerThread(List.Items[Count]).Stop; end; finally tcpServer.Threads.UnlockList; end; 这一段是向所有的链接发送信息。
wylton 2013-08-22
  • 打赏
  • 举报
回复
记录链接ip, 链接端口, 链接线程编号。 发送前区别当前链接是否有相同的ip, peer等,如果有就调用出来,发送内容。
tjs_125 2013-08-20
  • 打赏
  • 举报
回复
或者参考里面的广播消息函数的代码,如下: BroadcastMessage
procedure TfrmMain.BroadcastMessage( WhoFrom, TheMessage : String );
var
  Count: Integer;
  List : TList;
  EMote,
  Msg  : String;
begin
  Msg := Trim(TheMessage);

  EMote := Trim(memEMotes.Lines.Values[Msg]);

  if WhoFrom <> 'System' then
    Msg := WhoFrom + ': ' + Msg;

  if EMote <> '' then
    Msg := Format(Trim(EMote), [WhoFrom]);

  List := tcpServer.Threads.LockList;
  try
    for Count := 0 to List.Count -1 do
    try
      TIdPeerThread(List.Items[Count]).Connection.WriteLn(Msg);
    except
      TIdPeerThread(List.Items[Count]).Stop;
    end;
  finally
    tcpServer.Threads.UnlockList;
  end;
end;
tjs_125 2013-08-20
  • 打赏
  • 举报
回复
我刚才看了indy demo中的chat例子,里面是先在tcpServerConnect的事件里面把每一个连接上来得客服端都记下来,并保存在一个Clients里面(相当于客户端的管理列表),然后需要向某个已连接的客服端发送数据时,可遍历这个Clients,找出需要的客服端,然后 可以向下面这样发送消息
Client := Clients.Items[Index];
TIdPeerThread(Client.Thread).Connection.WriteLn(Msg);
不知道行不行?

1,594

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 网络通信/分布式开发
社区管理员
  • 网络通信/分布式开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧