indy9:
//一个简单的客户端连接信息数据结构
type PClient = ^TClient;
TClient = record // 客户端连接信息的类
clientip :String[50];//ip
Thread : Pointer;//指向一个客户端连接的指针
end;
Clients : TThreadList; //客户端连接列表
//连接事件
procedure TFrmDataCom.IdTCPServerConnect(AThread: TIdPeerThread);
var
NewClient: PClient;//新的连接
buf :array[0..1] of byte;
begin
new(NewClient);//分配内存
NewClient.Thread := AThread;
AThread.Data :=TObject(NewClient);
try
Clients.LockList.Add(NewClient); //在列表中添加新的对象
finally
Clients.UnlockList;
end;
end;
//断开事件
procedure TFrmDataCom.IdTCPServerDisconnect(AThread: TIdPeerThread);
var
ActClient: PClient;
id,index :integer;
begin
ActClient := Pointer(AThread.Data);
try
Clients.LockList.Remove(ActClient); //从list中移除客户端
finally
Clients.UnlockList;
end;
try
Dispose(ActClient);
AThread.Data := nil;
except
end;
end;
//接收数据事件
procedure TFrmDataCom.IdTCPServerExecute(AThread: TIdPeerThread);
var
recvResult :integer;
begin
if not AThread.Terminated and AThread.Connection.Connected then
begin
try
recvResult := AThread.Connection.Socket.Recv(tmprbuf,sizeof(tmprbuf));//接收数组
if(recvResult = -1) or (recvResult=0) then
begin
AThread.Connection.Disconnect;
end
else
begin
//处理数据
end;
except
AThread.Connection.Disconnect;
end;
end;
//关闭所有的连接
procedure TFrmDataCom.CloseAllClient;
var
i:Integer;
AThread: TIdPeerThread;
Client :PClient;
begin
try
with Clients.LockList do
begin
for i:=0 to Count-1 do
begin
try
AThread :=TIdPeerThread(PClient(Items[i]).Thread);
AThread.Connection.InputBuffer.Clear;
AThread.Connection.Disconnect;
except
end;
end;
end;
finally
Clients.UnlockList;
end;
IdTCPServer.Active :=false;
end;
indy当有客户端在线时候还要自己去disconn,真是无语。。。。。。。。。。不知道是不是这个样子???