IdTCPClient连接后服务器冻结



我有两个应用程序,它们使用TCPServer和TCPClient组件相互通信。服务器以隐藏模式启动:Application.ShowMainForm: = false;

只是系统托盘上的一个图标与用户交互。在运行服务器之后,如果我运行客户端并连接到服务器,则此冻结,但如果我将服务器属性Application.ShowMainForm更改为true,则一切都可以正常工作。这是我使用的代码:

客户端应用程序:

procedure TFormCliente.FormCreate(Sender: TObject);
begin
  try
    cliente.Connect;
  except
    hint1.ActivateHint(FormCliente,'Error.' + #13 +
     'Verify if server is running','VCall',5000); //hint1 is a Jed component
  end;
end;

服务器应用程序:

[...]
private
  FConexoes: TList;
[...]

type
  PClient   = ^TClient;
  TClient   = record
    PeerIP      : string[15];            { Client IP address }
    HostName    : String[40];            { Hostname }
    Connected,                           { Time of connect }
    LastAction  : TDateTime;             { Time of last transaction }
    AContext      : Pointer;             { Pointer to thread }
  end;
[...]
procedure TfrmServer.FormCreate(Sender: TObject);
begin
  FConexoes := TList.Create;
end;
procedure TFrmServer.FormDestroy(Sender: TObject);
begin
  FConexoes.Free;
end;
procedure TFrmServer.IdTCPServer1Connect(AContext: TIdContext);
var
  NewClient: PClient; 
begin
  GetMem(NewClient, SizeOf(TClient));
  NewClient.PeerIP      := AContext.Connection.Socket.Binding.PeerIP;
  NewClient.HostName    := GStack.HostByAddress(NewClient.PeerIP);
  NewClient.Connected   := Now;
  NewClient.LastAction  := NewClient.Connected;
  NewClient.AContext    := AContext;
  AContext.Data         := TObject(NewClient);
  ListView1.Items.Add.Caption:=NewClient.HostName;
end;

如果服务器表单是可见的,客户端主机名被添加到listview,但如果服务器表单不可见,并运行client和connect,服务器冻结,直到我杀死客户端进程。有人能帮我吗?

OnConnect事件中直接访问TListView不是线程安全的。这本身就会导致死锁和崩溃。试试这个:

type
  PClient   = ^TClient;
  TClient   = record
    PeerIP      : string;                { Client IP address }
    HostName    : String;                { Hostname }
    Connected,                           { Time of connect }
    LastAction  : TDateTime;             { Time of last transaction }
    AContext    : Pointer;               { Pointer to thread }
  end;
procedure TFrmServer.IdTCPServer1Connect(AContext: TIdContext);
var
  Client: PClient; 
begin
  New(Client);
  try
    Client.PeerIP      := AContext.Connection.Socket.Binding.PeerIP;
    Client.HostName    := GStack.HostByAddress(Client.PeerIP);
    Client.Connected   := Now;
    Client.LastAction  := Client.Connected;
    Client.AContext    := AContext;
    TThread.Synchronize(nil,
      procedure
      var
        Item: TListItem;
      begin
        Item := ListView1.Items.Add;
        Item.Data := Client;
        Item.Caption := Client.HostName;
      end
    );
  except
    Dispose(Client);
    raise;
  end;
  AContext.Data := TObject(Client);
end;
procedure TFrmServer.IdTCPServer1Disconnect(AContext: TIdContext);
var
  Client: PClient; 
begin
  Client := PClient(AContext.Data); 
  AContext.Data := nil;
  if Client = nil then Exit;
  TThread.Synchronize(nil,
    procedure
    var
      Item: TListItem;
    begin
      Item := ListView1.FindData(0, Client, True, False);
      if Item <> nil then
        Item.Delete;
    end
  );
  Dispose(NewClient);
end;

最新更新