1

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

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

客户端应用:

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;

如果服务器表单可见,则将客户端主机名添加到列表视图中,但如果服务器表单不可见,则运行 cliente 并连接,服务器将冻结,直到我终止客户端进程。谁能帮我?

4

1 回答 1

1

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

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;
于 2013-06-17T21:44:53.037 回答