关于IdTCPServer 我想IdTCPServer同时允许很多客户端连接,某个时候要向客户端中的某一个,主动发送一个数据,怎么做?最好给出例子 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 看indy的Demo中的chatprocedure 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; unit sm;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls;type TForm1 = class(TForm) Memo1: TMemo; Button1: TButton; IdTCPServer1: TIdTCPServer; procedure Button1Click(Sender: TObject); procedure IdTCPServer1Execute(AThread: TIdPeerThread); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);begin IdTCPServer1.DefaultPort:=6060; IdTCPServer1.Active:=true;end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);var fs:TFileStream; buf:Byte; i:integer; s:String;begin fs:=TFileStream.Create('a.mp3',fmCreate); try AThread.Connection.ReadStream(fs,-1,true); except showmessage('end'); end; fs.Destroy;end;end. 每个连接是一个TIdPeerThread,放在列表Threads: TThreadList;中, pt:TIdPeerThread pt:=idTcpServer.Threads[i];pt.Connection.WriteBuffer(...); 错了。pt:=idTcpServer.Threads.LockList.Items[i]; 小弟写的一个拉各朗日三点插值程序(源码),无法控制递减序列,请各位大虾帮忙看看 请高手帮看看,我的这个加密算法,哪里有问题?谢谢~ 转化 大虾:高分求教一个多线程处理的问题 开发的服务器端程序异常,有谁知道是怎么回事吗?(在线等待) 如何改变一个进程的窗口类名? timer的怪问题? 参数传址怎么写 MD5码是怎么回事? 标题说不清,请入内观看! 一个关于网络连接的问题,请各位专业人士指教! 如何在Treeview中保存两个字段的信息?
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;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPServer, StdCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
IdTCPServer1: TIdTCPServer;
procedure Button1Click(Sender: TObject);
procedure IdTCPServer1Execute(AThread: TIdPeerThread);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
IdTCPServer1.DefaultPort:=6060;
IdTCPServer1.Active:=true;
end;procedure TForm1.IdTCPServer1Execute(AThread: TIdPeerThread);
var
fs:TFileStream;
buf:Byte;
i:integer;
s:String;
begin
fs:=TFileStream.Create('a.mp3',fmCreate);
try
AThread.Connection.ReadStream(fs,-1,true);
except
showmessage('end');
end;
fs.Destroy;
end;
end. 每个连接是一个TIdPeerThread,放在列表Threads: TThreadList;中, pt:TIdPeerThread pt:=idTcpServer.Threads[i];
pt.Connection.WriteBuffer(...);
pt:=idTcpServer.Threads.LockList.Items[i];