4

Indy 9 TIdTCPServer (Delphi 2007 上) への現在のクライアント接続数を知りたい

これを与えるプロパティが見つからないようです。

サーバーの OnConnect/OnDisconnect イベントでカウンターをインクリメント/デクリメントしようとしましたが、クライアントが切断されても数値が減少することはありません。

助言がありますか?

4

4 に答える 4

9

現在アクティブなクライアントは、サーバーのThreadsプロパティに格納されていますTThreadListCountリストをロックし、そのプロパティを読み取り、リストのロックを解除するだけです。

procedure TForm1.Button1Click(Sender: TObject);
var
  NumClients: Integer;
begin
  with IdTCPServer1.Threads.LockList do try
    NumClients := Count;
  finally
    IdTCPServer1.Threads.UnlockList;
  end;
  ShowMessage('There are currently ' + IntToStr(NumClients) + ' client(s) connected');
end;

Indy 10 では、プロパティは次のThreadsプロパティに置き換えられましたContexts

procedure TForm1.Button1Click(Sender: TObject);
var
  NumClients: Integer;
begin
  with IdTCPServer1.Contexts.LockList do try
    NumClients := Count;
  finally
    IdTCPServer1.Contexts.UnlockList;
  end;
  ShowMessage('There are currently ' + IntToStr(NumClients) + ' client(s) connected');
end;
于 2011-04-02T05:58:12.710 に答える
4

OnConnect と OnDisconnect を使用してもうまくいかない理由はわかりませんが、TIdCustomTCPServer の子孫を作成しました。その DoConnect および DoDisconnect メソッドをオーバーライドし、TIdServerContext の独自の子孫 (接続を「提供する」スレッドの子孫) を作成して使用します。

TIdCustomTCPServer に独自の TIdServerContext クラスを認識させるには、次のようにします。

(編集Indy9 で機能させる方法を示す条件定義を追加)

type
// Conditional defines so that we can use the same ancestors as in Indy10 and we
// can use the same method signatures for DoConnect and DoDisconnect regardless 
// of the Indy version. Add other conditional defines as needed.
// Note: for INDY9 to be defined, you need to include the appropriate includes 
// from Indy, or define it in your own include file.
{$IFDEF INDY9}  
  TIdContext = TIdPeerThread;
  TIdServerContext = TIdContext;
  TIdCustomTCPServer = TIdTCPServer;
{$ENDIF}

  TOurContext = class(TIdServerContext)
  private
    FConnectionId: cardinal;
  public
    property ConnectionId: cardinal ...;
  end;

...

constructor TOurServer.Create(aOwner: TComponent);
begin
  inherited Create(aOwner);

  ...
  {$IFDEF INDY10_UP}
    ContextClass := TOurContext;
  {$ELSE}
    ThreadClass := TOurContext;
  {$ENDIF}
  ...
end;

TIdCustomTCPServer の子孫の DoConnect オーバーライドで、コンテキスト クラスの ConnectionID を一意の値に設定します。

procedure TOurServer.DoConnect(AContext: TIdContext);
var
  OurContext: TOurContextabsolute AContext;
begin
  Assert(AContext is TOurContext);
  HandleGetNewConnectionID(OurContext, OurContext.FConnectionID);

  inherited DoConnect(AContext);

  ...

end;

DoDisconnect オーバーライドは ConnectionID をクリアします。

procedure TOurServer.DoDisconnect(AContext: TIdContext);
var
  OurContext: TOurContextabsolute AContext;
begin
  Assert(AContext is TOurContext);
  OurContext.FConnectionID := 0;

  ...

  inherited DoDisconnect(AContext);
end;

現在の接続数をいつでも取得できるようになりました。

function TOurServer.GetConnectionCount: Integer;
var
  i: Integer;
  CurrentContext: TOurContext;
  ContextsList: TList;
begin
  MyLock.BeginRead;
  try
    Result := 0;

    if not Assigned(Contexts) then
      Exit;

    ContextsList := Contexts.LockList;
    try

      for i := 0 to ContextsList.Count - 1 do
      begin
        CurrentContext := ContextsList[i] as TOurContext;

        if CurrentContext.ConnectionID > 0 then
          Inc(Result);
      end;

    finally
      Contexts.UnLockList;
    end;
  finally
    MyLock.EndRead;
  end;
end;
于 2011-04-01T14:44:07.347 に答える
3

からカウンターをインクリメント/デクリメントするのはどうですかOnExecute(またはDoExecuteそれをオーバーライドする場合)?それは間違いありません!

を使用InterlockedIncrementInterlockedDecrement、カウンターを保護するためのクリティカルセクションさえ必要ない場合。

于 2011-04-01T13:04:40.213 に答える
2

これはIndy9で機能するはずですが、最近はかなり古くなっており、バージョンで何かが壊れている可能性があります。利用可能な最新のIndy9に更新してみてください。

Indy 10を使用して簡単なテストを行いました。これは、OnConnect/OnDisconnectイベントハンドラーの単純なインターロックされたインクリメント/デクリメントで非常にうまく機能します。これは私のコードです:

//closes and opens the server, which listens at port 1025, default values for all properties
procedure TForm2.Button1Click(Sender: TObject);
begin
  IdTCPServer1.Active := not IdTCPServer1.Active;
  UpdateUI;
end;

procedure TForm2.FormCreate(Sender: TObject);
begin
  UpdateUI;
end;

//Just increment the count and update the UI
procedure TForm2.IdTCPServer1Connect(AContext: TIdContext);
begin
  InterlockedIncrement(FClientCount);
  TThread.Synchronize(nil, UpdateUI);
end;

//Just decrement the count and update the UI
procedure TForm2.IdTCPServer1Disconnect(AContext: TIdContext);
begin
  InterlockedDecrement(FClientCount);
  TThread.Synchronize(nil, UpdateUI);
end;

//Simple 'X' reply to any character, A is the "command" to exit
procedure TForm2.IdTCPServer1Execute(AContext: TIdContext);
begin
  AContext.Connection.IOHandler.Writeln('Write anything, but A to exit');
  while AContext.Connection.IOHandler.ReadByte <> 65 do
    AContext.Connection.IOHandler.Write('X');
  AContext.Connection.IOHandler.Writeln('');
  AContext.Connection.IOHandler.Writeln('Good Bye');
  AContext.Connection.Disconnect;
end;

//Label update with server status and count of connected clients 
procedure TForm2.UpdateUI;
begin
  Label1.Caption := Format('Server is %s, %d clients connected', [
    IfThen(IdTCPServer1.Active, 'Open', 'Closed'), FClientCount]);
end;

次に、telnetでいくつかのクライアントを開きます。

3つの接続されたクライアント

次に、1つのクライアントを閉じます

2つの接続されたクライアント

それでおしまい。

INDY10はDelphi2007で使用できます。私の主なアドバイスは、とにかくアップグレードすることです。

于 2011-04-01T16:46:24.613 に答える