gethostbyaddr 太慢

发布于 2024-09-14 04:02:22 字数 508 浏览 5 评论 0原文

我使用以下代码,结果是正确的,但 gethostbyaddr 需要大约 30 秒。

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;   

I use the following code, results are correct, but gethostbyaddr takes around 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;   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

画尸师 2024-09-21 04:02:22

这不太可能是您的代码的问题(除非 WSAStartup 特别慢)。

我要做的第一件事是输出代码中每一行之间的时间(最好是毫秒,我认为使用 GetTickCount),以找出确切地说,时间都花在哪里了。

gethostbyaddr 可能必须转到远程 DNS 计算机才能将 IP 地址解析为主机名。

如果您的网络设置不佳,或者包含该地址的 DNS 服务器位于西藏山区的偏远地区,则解析需要一些时间。

从命令行输入:(

nslookup x.x.x.x

其中 xxxx 是您感兴趣的 IP 地址)并查看需要多长时间。

根据您在下面标尺线之间的评论:


我正在仅使用 3 台机器的 LAN 上工作。此外,该网络未连接到互联网。 是瞬时的,需要 16 秒(+/- 一些毫秒)

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

仅行: while:

GetHostByName(PChar(HostName));

。以下是 Ping(即时输出)和 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.

我认为您的问题在于超时。看来您的网络设置可以进行 DNS 名称解析,但不能进行 IP 反向解析。

当您只需输入 nslookup 时,它应该会向您显示它正在尝试使用的 DNS 服务器,这可能会给您提供线索。

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

将名称解析为 IP 地址可能不会通过 DNS 发出,而是使用本地信息进行处理。

这就是我根据当前信息所能为您提供的尽可能多的帮助。由于现在这看起来更像是一个超级用户问题,而不是 StackOverflow,所以我会将其推到那里。

That's unlikely to be an issue with your code (unless WSAStartup is particularly slow).

The first thing I would do is to output the time (preferably to the millisecond, with GetTickCount, I think) in between each of those lines in your code, to find out exactly where the time is being spent.

The gethostbyaddr may well have to go out to a remote DNS machine to resolve the IP address into a hostname.

If your network is set up badly, or the DNS server containing that address is in the remote regions of the Tibetan mountains for example, resolution will take some time.

From your command line, enter:

nslookup x.x.x.x

(where x.x.x.x is the IP address you're interested in) and see how long that takes.

Based on your comment between the ruler lines below:


I am working on LAN with just 3 machines. Also that network is not connected to the internet. It takes 16 secs (+/- some millisecs) for only the line:

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

while:

GetHostByName(PChar(HostName));

is instantaneous. Below 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 that timeout. It appears your network is set up okay for DNS name resolution but not IP reverse resolution.

When you just type nslookup, it should show you your DNS server that it's trying to use and this will probably give you a clue.

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

It may be that resolving names to IP addresses doesn't go out through DNS but is instead handled with local information.

That's about as much help as I can give you with the current information. Since this now seems very much a SuperUser question now rather than StackOverflow, I'll nudge it over to there.

那片花海 2024-09-21 04:02:22

Windows 根据主机和 LAN 的配置方式尝试不同的方式来执行主机名解析。请参阅 http://technet.microsoft.com/en-us/library/bb727005 .aspx
您不应在未正确配置的 LAN 中测试该代码,无论是使用 DNS 服务器(或至少是 WINS 服务器)还是正确的主机文件。否则,您将无法在正确配置的 LAN 中获得预期的结果。

Windows attempts different ways to perform host name resolution depending on the way your hosts and LAN are configured. See http://technet.microsoft.com/en-us/library/bb727005.aspx.
You should not test that code in a LAN which is not correctly configured, either using a DNS server (or at least a WINS one) or correct hosts files. Otherwise you could not get the result you'd expect in a properly configured LAN.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文