Eliminate a host domain to an IP address

zhaozj2021-02-11  207

Resolve a host domain to an IP address. Using the Winsock unit; 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 (add) 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; convend;

Please in the online state when testing.

Test code: varIP: string; DNS: string; begin DNS: = InputBox ( 'Enter the DNS domain name', 'hostname:', ''); if HostToIp (DNS, IP) then showmessage (IP); end; Author: Latfi Baran

When I test Kingron gives the usage, Hostent is always = nil. Later, I was adding WSAStartup before using gethostbyname (...), and finally everything OK. No getdressbyname in the Winsock unit ()

转载请注明原文地址:https://www.9cbs.com/read-3846.html

New Post(0)