下面是我写的给所有连接的客户端发消息的代码:Var i:Integer; Msg:String; Client:TSimpleClient; APeer:TIdPeerThread; AList:TList; begin AList := IdTCPServer.Threads.LockList; try if AList.Count>0 then begin Msg := InputBox('发送消息','请输入',''); //给所有客户发消息 for i := 0 to AList.Count -1 do begin APeer := TIdPeerThread(AList.Items[i]); APeer.Connection.WriteLn(Msg); end; //TIdPeerThread(IdTCPServer.Threads.LockList.Items[0]).Connection.WriteLn(Msg); end; finally IdTCPServer.Threads.UnlockList(); end; end;就是不知道在客户端怎么知道有消息??若象Indy本身带的Demo中的Chart程序的客户端放在时钟中,好象容易造程序没有反应(放那个AntiFreez也没有用)!
在客户端可以专门建立一个线程来接收消息: procedure TClientHandleThread.Execute; begin while not Terminated do begin if not frmjianting.IdTCPClient1.Connected then Terminate else try recieve := frmjianting.IdTCPClient1.ReadLn(); Synchronize(HandleInput); //线程同步。 except exit; end; end; end;
begin
LbLog.Items.Add('来自主机 '
+ AThread.Connection.Socket.Binding.PeerIP
+ ' 的连接请求已被接纳!');
AThread.Connection.WriteLn('100: 欢迎连接到服务器!');
end;
i:Integer;
Msg:String;
Client:TSimpleClient;
APeer:TIdPeerThread;
AList:TList;
begin
AList := IdTCPServer.Threads.LockList;
try
if AList.Count>0 then
begin
Msg := InputBox('发送消息','请输入','');
//给所有客户发消息
for i := 0 to AList.Count -1 do
begin
APeer := TIdPeerThread(AList.Items[i]);
APeer.Connection.WriteLn(Msg);
end;
//TIdPeerThread(IdTCPServer.Threads.LockList.Items[0]).Connection.WriteLn(Msg);
end;
finally
IdTCPServer.Threads.UnlockList();
end;
end;就是不知道在客户端怎么知道有消息??若象Indy本身带的Demo中的Chart程序的客户端放在时钟中,好象容易造程序没有反应(放那个AntiFreez也没有用)!
procedure TClientHandleThread.Execute;
begin
while not Terminated do
begin
if not frmjianting.IdTCPClient1.Connected then
Terminate
else
try
recieve := frmjianting.IdTCPClient1.ReadLn();
Synchronize(HandleInput); //线程同步。
except
exit;
end;
end;
end;