如何从服务端主动断开已经连接的客户端(附部分代码)?(200分)

  • 主题发起人 主题发起人 wv990
  • 开始时间 开始时间
W

wv990

Unregistered / Unconfirmed
GUEST, unregistred user!
type
PClient = ^TClient;
TClient = record
PeerIP: string[15]; { 客户端IP地址 }
Thread: Pointer; { Pointer to thread }
end;

procedure Tmain.IdTCPServer1Connect(AContext: TIdContext);
var
sendcmd: string;
NewClient: PClient;
begin
GetMem(NewClient, SizeOf(TClient));
NewClient.PeerIP := AContext.Binding.PeerIP;
NewClient.Thread := AContext;
AContext.Data := TObject(NewClient);
try
Clients.LockList.Add(NewClient);
finally
Clients.UnlockList;
end;
end;

procedure Tmain.RzBitBtn9Click(Sender: TObject);
var
Bindings: TIdSocketHandles;
i, ClientsCount: integer;
begin //服务器启动
if not IdTCPServer1.Active then
begin
RzBitBtn9.Caption := '停止服务';
Bindings := TIdSocketHandles.Create(idTCPServer1);
try
try
idTCPServer1.Bindings := Bindings;
idTCPServer1.Active := True;
except on E: Exception do
ShowMessage(E.Message);
end;
finally
Bindings.Free;
end;
Clients := TThreadList.Create;
Clients.Duplicates := dupAccept;
RefreshListDisplay;
end
else
begin //服务器停止,断开客户端
try
ClientsCount := Clients.LockList.Count;
finally
Clients.UnlockList;
end;
if (ClientsCount > 0) then
begin
case MessageBox(Handle,
PChar('有连接的客户端,确定要停止服务吗?'),
PChar('提示信息'),
MB_ICONINFORMATION or MB_YESNO or MB_DEFBUTTON2) of
IDYES:
begin
RzBitBtn9.Caption := '启动服务';
try
for I := 0 to Clients.LockList.Count - 1 do
begin

如何在此次编写代码断开已经连接的客户端??????????????????????????????????

end;
finally
Clients.UnlockList
end;
end;
end;
end;
end;
 
接受答案了.
 
后退
顶部