TIdStack.HostByAddress()
クライアントのリモートホスト名を取得するために使用します。例:
adr := GStack.HostByAddress(IP);
TIdStack.IncUsage()
そうは言っても、 andを呼び出す必要はありません。コンストラクタとデストラクタでそれぞれ処理するためですTIdStack.DecUsage()
。TIdTCPServer
しかし、さらに重要なことは、 への直接アクセスはTMemo
スレッドセーフではありません。TIdTCPServer
これはマルチスレッド コンポーネントであることを忘れないでください。OnConnect
イベント (およびイベント)は、メイン スレッドではなく、ワーカー スレッドOnDisconnect
でOnExecute
実行されます。代わりに、メイン スレッドで 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;