用IdTCPServer做服务端程序,如果客户端全部断开就可以停止服务,如果还存在客户端连接就会报错。服务端的连接我用了一个TThreadList变量保存,这是程序退出的代码:procedure TData.DataModuleDestroy(Sender: TObject);
var
  I:Integer;
  Client:PClient;
  CThread:TIdPeerThread;
begin
 //如果加入下面这一段代码,程序就无法退出,如果不加就报错。(我指存在客户端连接未断开的情况)
  //Clients是保存客户端连接的TThreadList类。
  for I:=0 to Clients.LockList.Count-1 do
  begin
    Client:=Clients.LockList.Items[I];
    CThread:=Client.Thread;
    CThread.Connection.DisconnectSocket;
  end;
  //如果加入上面这一段代码,程序就无法退出,如果不加就报错。(我指存在客户端连接未断开的情况)  ADOConnection.Close;
  IdTCPServer.Active:=False;
end;