TUniConnection: Name of server from IP address

Discussion of open issues, suggestions and bugs regarding UniDAC (Universal Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Giovanna
Posts: 25
Joined: Tue 21 Jul 2009 07:44
Location: Italy

TUniConnection: Name of server from IP address

Post by Giovanna » Mon 27 Aug 2012 06:21

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

AndreyZ

Re: TUniConnection: Name of server from IP address

Post by AndreyZ » Mon 27 Aug 2012 11:51

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.

Post Reply