Solved

Get host name from IP address!

Posted on 1999-01-19
7
253 Views
Last Modified: 2010-04-06
How to do it?
0
Comment
Question by:mingwei
  • 4
  • 3
7 Comments
 
LVL 7

Expert Comment

by:BlackMan
ID: 1362436
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;

0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1362437
Oops, remove the line AddrStr := '136.148.3.10'; from the function...
0
 

Author Comment

by:mingwei
ID: 1362438
GetHostByAddr(@Addr.s_addr,4,PF_INET);
return nil
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 7

Accepted Solution

by:
BlackMan earned 30 total points
ID: 1362439
Sorry, I cut it out of a larger unit. You need to initialize WinSock first to make it work.

uses Winsock;

Var
  VerReqd      : Word;
  WSAData      : TWSAData;
  WSErr      : Word;

function HostByAddr(AddrStr: String): String;
Var
  Addr: TInAddr;
  Host: PHostEnt;
begin
  VerReqd := $0101;
  WSErr := WSAStartup(VerReqd, WSAData);

  Addr.s_addr := Inet_Addr(PChar(AddrStr));
  Host := GetHostByAddr(@Addr.s_addr,4,PF_INET);
  Result := Host^.h_name;
end;


0
 

Author Comment

by:mingwei
ID: 1362440
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.

0
 
LVL 7

Expert Comment

by:BlackMan
ID: 1362441
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 www.experts-exchange.com. You need to be connected to read the names, there is no cache with names/addresses..

0
 

Author Comment

by:mingwei
ID: 1362442
Oh,I know!
Very thanks.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video discusses moving either the default database or any database to a new volume.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now