Page 1 of 1
TUniConnection: Name of server from IP address
Posted: Mon 27 Aug 2012 06:21
by Giovanna
Hi,
I have TUniConnection.Server with an IP address, how can i translate it
in the server name? I work in delphi Xe2.
Thanks,
Giovanna
Re: TUniConnection: Name of server from IP address
Posted: Mon 27 Aug 2012 11:51
by AndreyZ
Hello,
You can use the gethostbyaddr Windows function to get the host name by the IP address. Here is an example:
Code: Select all
function GetHostNameByIP(IP: string): string;
var
WSAData: TWSAData;
SocketAddr: TSockAddr;
HostEnt: PHostEnt;
begin
WSAStartup($0202, WSAData);
SocketAddr.sin_addr.S_addr := inet_addr(PAnsiChar(AnsiString(IP)));
HostEnt := gethostbyaddr(@SocketAddr.sin_addr.S_addr, 4, AF_INET);
if HostEnt <> nil then
Result := string(HostEnt^.h_name)
else
Result := '';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
ShowMessage(GetHostNameByIP('127.0.0.1'));
end;
To use this code, you should add the WinSock unit to the USES clause of your unit.