Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Resolve Host address to IP numbers

Title: Resolve Host address to IP numbers Question: Describe a function that shows how to resolve Host address to IP numbers. Answer: Declare Winsock in the uses clause ............ function HostToIP(Name: string; var Ip: string): Boolean; var wsdata : TWSAData; hostName : array [0..255] of char; hostEnt : PHostEnt; addr : PChar; begin WSAStartup ($0101, wsdata); try gethostname (hostName, sizeof (hostName)); StrPCopy(hostName, Name); hostEnt := gethostbyname (hostName); if Assigned (hostEnt) then if Assigned (hostEnt^.h_addr_list) then begin addr := hostEnt^.h_addr_list^; if Assigned (addr) then begin IP := Format ('%d.%d.%d.%d', [byte (addr [0]), byte (addr [1]), byte (addr [2]), byte (addr [3])]); Result := True; end else Result := False; end else Result := False else begin Result := False; end; finally WSACleanup; end end; ................................ You can try this function placing a EditBox and a Button and A Label on a form and adding this code to the OnClick event of the button: procedure TForm1.Button1Click(Sender: TObject); var IP: string; begin if HostToIp(Edit1.Text, IP) then Label1.Caption := IP; end;