簡體   English   中英

如何獲取delphi Xe2 Indy 10套接字服務器對等主機名

[英]how to get delphi Xe2 Indy 10 socket server peer host name

這是我的代碼...

procedure TMainForm.tsConnect(AContext: TIdContext);
var
  s, INstr, adr:string;
  port: Integer;
begin
  with TMyContext(AContext) do
  begin
    Con := Now;
    if (Connection.Socket <> nil) then
      IP :=Connection.Socket.Binding.PeerIP;
    port:=Connection.Socket.Binding.PeerPort;
    s:=IntToStr(Connection.Socket.Binding.PeerPort);
    TIdStack.IncUsage();
    try
      adr:= GStack.HostName;
    finally
      TIdStack.DecUsage;
    end;

    INstr := Connection.IOHandler.ReadLn;
    Nick := INstr;

    if Nick <> '' then
    begin
      memo1.Lines.Add('Opened  <'+Nick + '>  '+adr+' '+IP+':'+s+'      '+DAteTimeToStr(now));
      //SendNicks;
    end else
    begin
      Connection.IOHandler.WriteLn('No Nick provided! Goodbye.');
      Connection.Disconnect;
    end;
  end;
end;

GStack.HostName給出我的服務器名稱,如何獲取客戶端主機名?

使用TIdStack.HostByAddress()獲取客戶端的遠程主機名,例如:

adr := GStack.HostByAddress(IP);

如此說來,您無需調用TIdStack.IncUsage()TIdStack.DecUsage()因為TIdTCPServer分別在其構造函數和析構函數中為您處理該問題。 但更重要的是,您對TMemo直接訪問不是線程安全的。 請記住, TIdTCPServer是一個多線程組件。 OnConnect事件(以及OnDisconnectOnExecute在工作線程中運行,而不是在主線程中運行。UI訪問必須在主線程中完成。

嘗試這個:

procedure TMainForm.tsConnect(AContext: TIdContext);
var
  INstr, adr: string;
  port: Integer;
begin
  with TMyContext(AContext) do
  begin
    Con := Now;
    IP := Connection.Socket.Binding.PeerIP;
    port := Connection.Socket.Binding.PeerPort;

    adr := GStack.HostByAddress(IP);

    INstr := Connection.IOHandler.ReadLn;
    Nick := INstr;

    if Nick <> '' then
    begin
      TThread.Synchronize(nil,
        procedure
        begin
          memo1.Lines.Add('Opened  <' + Nick + '>  ' + adr + ' ' + IP + ':' + IntToStr(port) + '      ' + DateTimeToStr(Con));
        end
      );
      //SendNicks;
    end else
    begin
      Connection.IOHandler.WriteLn('No Nick provided! Goodbye.');
      Connection.Disconnect;
    end;
  end;
end;

或者:

uses
  ..., IdSync;

type
  TMemoNotify = class(TIdNotify)
  protected
    FMsg: String;
    procedure DoNotify; override;
  end;

procedure TMemoNotify.DoNotify;
begin
  MainForm.Memo1.Lines.Add(FMsg);
end;

procedure TMainForm.tsConnect(AContext: TIdContext);
var
  INstr, adr: string;
  port: Integer;
begin
  with TMyContext(AContext) do
  begin
    Con := Now;
    IP := Connection.Socket.Binding.PeerIP;
    port := Connection.Socket.Binding.PeerPort;

    adr := GStack.HostByAddress(IP);

    INstr := Connection.IOHandler.ReadLn;
    Nick := INstr;

    if Nick <> '' then
    begin
      with TMemoNotify.Create do
      begin
        FMsg := 'Opened  <' + Nick + '>  ' + adr + ' ' + IP + ':' + IntToStr(port) + '      ' + DateTimeToStr(Con);
        Notify;
      end;
      //SendNicks;
    end else
    begin
      Connection.IOHandler.WriteLn('No Nick provided! Goodbye.');
      Connection.Disconnect;
    end;
  end;
end;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM