如何获取delphi Xe2 Indy 10 socket服务器对等主机名
这是我的密码如何获取delphi Xe2 Indy 10 socket服务器对等主机名,delphi,delphi-xe2,indy,Delphi,Delphi Xe2,Indy,这是我的密码 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.Pe
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;
程序TMainForm.tsConnect(AContext:TIdContext);
变量
s、 仪器,adr:字符串;
端口:整数;
开始
使用TMyContext(AContext)do
开始
Con:=现在;
如果(Connection.Socket nil),则
IP:=Connection.Socket.Binding.PeerIP;
端口:=Connection.Socket.Binding.PeerPort;
s:=IntToStr(Connection.Socket.Binding.PeerPort);
TIdStack.IncUsage();
尝试
adr:=GStack.HostName;
最后
TIdStack.decussion;
结束;
INstr:=Connection.IOHandler.ReadLn;
尼克:=仪表;
如果是尼克,那么
开始
备忘录1.行。添加('Opened'+adr+''+IP+:'+s+''+DAteTimeToStr(现在));
//森德尼克斯;
结束其他
开始
Connection.IOHandler.WriteLn('没有尼克提供!再见');
连接。断开;
结束;
结束;
结束;
GStack.HostName提供我的服务器名称,如何获取客户端主机名?使用
TIdStack.HostByAddress()
获取客户端的远程主机名,例如:
adr := GStack.HostByAddress(IP);
话虽如此,您不需要调用TIdStack.IncUsage()
和TIdStack.decussion()
,因为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;
程序TMainForm.tsConnect(AContext:TIdContext);
变量
仪器,adr:字符串;
端口:整数;
开始
使用TMyContext(AContext)do
开始
Con:=现在;
IP:=Connection.Socket.Binding.PeerIP;
端口:=Connection.Socket.Binding.PeerPort;
adr:=GStack.HostByAddress(IP);
INstr:=Connection.IOHandler.ReadLn;
尼克:=仪表;
如果是尼克,那么
开始
TThread.Synchronize(nil,
程序
开始
备忘录1.行。添加('Opened'+adr+'+IP+':'+IntToStr(端口)+'+DateTimeToStr(Con));
结束
);
//森德尼克斯;
结束其他
开始
Connection.IOHandler.WriteLn('没有尼克提供!再见');
连接。断开;
结束;
结束;
结束;
或者:
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;
使用
…,IdSync;
类型
tmemotify=class(tidtify)
受保护的
FMsg:字符串;
程序不通知;覆盖;
结束;
程序tmemotify.DoNotify;
开始
主窗体备忘录1.行添加(FMsg);
结束;
程序TMainForm.tsConnect(AContext:TIdContext);
变量
仪器,adr:字符串;
端口:整数;
开始
使用TMyContext(AContext)do
开始
Con:=现在;
IP:=Connection.Socket.Binding.PeerIP;
端口:=Connection.Socket.Binding.PeerPort;
adr:=GStack.HostByAddress(IP);
INstr:=Connection.IOHandler.ReadLn;
尼克:=仪表;
如果是尼克,那么
开始
使用tmemotify.createdo
开始
FMsg:=“已打开”+adr++“+IP+”:“+IntToStr(端口)+”+DateTimeToStr(Con);
通知
结束;
//森德尼克斯;
结束其他
开始
Connection.IOHandler.WriteLn('没有尼克提供!再见');
连接。断开;
结束;
结束;
结束;
使用TIdStack.HostByAddress()
获取客户端的远程主机名,例如:
adr := GStack.HostByAddress(IP);
话虽如此,您不需要调用TIdStack.IncUsage()
和TIdStack.decussion()
因为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;
程序TMainForm.tsConnect(AContext:TIdContext);
变量
仪器,adr:字符串;
端口:整数;
开始
使用TMyContext(AContext)do
开始
Con:=现在;
IP:=Connection.Socket.Binding.PeerIP;
端口:=Connection.Socket.Binding.PeerPort;
adr:=GStack.HostByAddress(IP);
INstr:=Connection.IOHandler.ReadLn;
尼克:=仪表;
如果是尼克,那么
开始
TThread.Synchronize(nil,
程序
开始
备忘录1.行。添加('Opened'+adr+'+IP+':'+IntToStr(端口)+'+DateTimeToStr(Con));
结束
);
//森德尼克斯;
结束其他
开始
Connection.IOHandler.WriteLn('没有尼克提供!再见');
连接。断开;
结束;
结束;
结束;
或者:
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;
使用
…,IdSync;
类型
tmemotify=class(tidtify)
受保护的
FMsg:字符串;
程序不通知;覆盖;
结束;
程序tmemotify.DoNotify;
开始
主窗体备忘录1.行添加(FMsg);
结束;
程序TMainForm.tsConnect(AContext:TIdContext);
变量
仪器,adr:字符串;
端口:整数;
开始
使用TMyContext(AContext)do
开始
Con:=现在;
IP:=Connection.Socket.Binding.PeerIP;
端口:=Connection.Socket.Binding.PeerPort;
adr:=GStack.HostByAddress(IP);
INstr:=Connection.IOHandler.ReadLn;
尼克:=仪表;
如果是尼克,那么
开始
使用tmemotify.createdo
开始
FMsg:=“已打开”+adr++“+IP+”:“+IntToStr(端口)+”+DateTimeToStr(Con);
通知
结束;
//森德尼克斯;
结束其他
开始
Connection.IOHandler.WriteLn('没有尼克提供!再见');
连接。断开;
结束;
结束;
结束;