We help IT Professionals succeed at work.

Get host name from IP address!

mingwei
mingwei asked
on
706 Views
Last Modified: 2010-04-06
How to do it?
Comment
Watch Question

Commented:
Use the GetHostByAddr function, like this:

uses Winsock;

function HostByAddr(AddrStr: String): String;
  Addr: TInAddr;
  Host: PHostEnt;
begin
  AddrStr := '136.148.3.10';
  Addr.s_addr := Inet_Addr(PChar(AddrStr));
  Host := GetHostByAddr(@Addr.s_addr,4,PF_INET);
  Result := Host^.h_name;
end;

Commented:
Oops, remove the line AddrStr := '136.148.3.10'; from the function...

Author

Commented:
GetHostByAddr(@Addr.s_addr,4,PF_INET);
return nil
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
But I want to say this way just return the hosts name
from local HOSTS files.
If I want to get hosts name from Internet how can I read
information from IP pack?
If you have any source tell me about that!
Thanks a lot.

Commented:
This code is not specific to the HOSTS file, it uses the normal TCP/IP conventions. This means that if you want to use it on www addresses, you must have a proper DNS configured in your TCP/IP settings, which you'd probably allready have if you are able to access https://www.experts-exchange.com. You need to be connected to read the names, there is no cache with names/addresses..

Author

Commented:
Oh,I know!
Very thanks.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.