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
TUniConnection: Name of server from IP address
-
AndreyZ
Re: TUniConnection: Name of server from IP address
Hello,
You can use the gethostbyaddr Windows function to get the host name by the IP address. Here is an example:To use this code, you should add the WinSock unit to the USES clause of your unit.
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;