简体   繁体   English

如何获取delphi Xe2 Indy 10套接字服务器对等主机名

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

Here is my code... 这是我的代码...

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 gives name of my server, how to get client host name? GStack.HostName给出我的服务器名称,如何获取客户端主机名?

Use TIdStack.HostByAddress() to get the client's remote hostname, eg: 使用TIdStack.HostByAddress()获取客户端的远程主机名,例如:

adr := GStack.HostByAddress(IP);

With that said, you do not need to call TIdStack.IncUsage() and TIdStack.DecUsage() because TIdTCPServer handles that for you in its constructor and destructor, respectively. 如此说来,您无需调用TIdStack.IncUsage()TIdStack.DecUsage()因为TIdTCPServer分别在其构造函数和析构函数中为您处理该问题。 But more importanly, your direct access of the TMemo is not thread-safe. 但更重要的是,您对TMemo直接访问不是线程安全的。 Remember that TIdTCPServer is a multi-threaded component. 请记住, TIdTCPServer是一个多线程组件。 The OnConnect event (and OnDisconnect and OnExecute runs in a worker thread, not the main thread. UI access must be done in the main thread instead. OnConnect事件(以及OnDisconnectOnExecute在工作线程中运行,而不是在主线程中运行。UI访问必须在主线程中完成。

Try this: 尝试这个:

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;

Alternatively: 或者:

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