Gethostbyaddr is too slow

I am using the following code, the results are correct, but gethostbyaddr takes about 30 seconds.

 function IPAddrToName(IPAddr: string): string; var SockAddrIn: TSockAddrIn; HostEnt: PHostEnt; WSAData: TWSAData; begin WSAStartup($101, WSAData); SockAddrIn.sin_addr.s_addr := inet_addr(PChar(IPAddr)); HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET); if HostEnt <> nil then Result := StrPas(Hostent^.h_name) else Result := ''; end; 

Allan Relations

+4
source share
2 answers

This is unlikely to be a problem for your code (unless WSAStartup especially slow).

The first thing I would like to do is print the time (preferably in a millisecond, with GetTickCount , I think) between each of these lines in your code to find out exactly where the time is.

gethostbyaddr may need to go to the remote DNS machine to resolve the IP address in the host name.

If your network is configured poorly or the DNS server containing this address is located in remote regions of the Tibetan mountains, for example, the resolution will take some time.

At the command prompt, type:

 nslookup xxxx 

(where xxxx is the IP address you are interested in) and see how long it takes.

Based on your comment between the lines of the line below:


I work on a local network in just 3 machines. Also, this network is not connected to the Internet. It takes 16 seconds (+/- a few milliseconds) for the line only:

 HostEnt := gethostbyaddr(@SockAddrIn.sin_addr.S_addr, 4, AF_INET); 

a

 GetHostByName(PChar(HostName)); 

instantly. The following is the output of Ping (instant output) and nslookup:

 c:\> ping 192.168.1.22 Reply from 192.168.1.22: bytes=32 time<1ms TTL=128 Packets: Sent = 4, Received = 4, Lost = 0 (0% loss) c:\> nslookup 192.168.1.22 DNS request timed out. 

I think your problem is with this timeout. Your network seems to be configured to resolve DNS names, but not for IP reverse resolution.

When you just type nslookup , it should show your DNS server that it is trying to use, and that will probably give you the key.

 c:\pax> nslookup Default Server: pax01.neveryoumind.com Address: 9.190.230.75 

It is possible that name resolution for IP addresses does not go out through DNS, but instead is processed by local information.

This is about the same help as I can give you current information. Since it now looks like a SuperUser question, not StackOverflow, I push it there.

+3
source

Windows tries to use different methods for resolving a host name depending on how your hosts and LAN are configured. See http://technet.microsoft.com/en-us/library/bb727005.aspx . You should not test this code on a LAN that is not configured correctly, either using a DNS server (or at least a WINS-one) or the correct host files. Otherwise, you would not be able to get the expected result on a properly configured local network.

+1
source

All Articles