Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Get host name from IP address!

Posted on 1999-01-19
7
261 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 110
Downloading email attachments 2 79
Delphi 2 68
Firemonkey android show image from resource ? 1 46
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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 an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

808 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