IP/Host Resolver

Wednesday, July 11, 2012 0 comments
Program that converts IP to host and host to IP
A simple use of Windows API for playing with networks. Today I thought of  making a simple program with the help of Windows API that can return your hostname if you enter IP address and return your IP address if you enter your hostname.
 This program can be very useful for some project where they need user IP address or hostname for processing.



We have used the WinSock API to achieve this.

Here's the code written in Delphi

// Function to get the Host from IP address
function TFrmMain.IPAddrToName(IPAddr: string): string;
var
  SockAddrIn: TSockAddrIn;
  HostEnt: PHostEnt;
  WSAData: TWSAData;
begin
  WSAStartup($101, WSAData);
  SockAddrIn.sin_addr.s_addr := inet_addr(PAnsiChar(AnsiString(IPAddr)));
  HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET);
  if HostEnt <> nil then
    Result := StrPas(Hostent^.h_name)
  else
    Result := '';
end;


// Function to get the IP Address from a Host

function TFrmMain.GetIPFromHost(HostName: string): string;
type
  TaPInAddr = array[0..10] of PInAddr;
  PaPInAddr = ^TaPInAddr;
var
  phe: PHostEnt;
  pptr: PaPInAddr;
  i: Integer;
  GInitData: TWSAData;
begin
  WSAStartup($101, GInitData);
  Result := '';
  phe := GetHostByName(PAnsiChar(AnsiString((HostName))));
  if phe = nil then Exit;
  pPtr := PaPInAddr(phe^.h_addr_list);
  i := 0;
  while pPtr^[i] <> nil do
  begin
    Result := inet_ntoa(pptr^[i]^);
    Inc(i);
  end;
  WSACleanup;
end;



For complete project source code and exceutable
Refer this site Ip-Host Resolver or Softpedia.com
https://sourceforge.net/projects/iphostresolver

0 comments:

Post a Comment

Recent Posts

WebInfo

 

©Copyright 2011 WebInfo