gethostbyaddr 太慢
我使用以下代码,结果是正确的,但 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不太可能是您的代码的问题(除非
WSAStartup
特别慢)。我要做的第一件事是输出代码中每一行之间的时间(最好是毫秒,我认为使用 GetTickCount),以找出确切地说,时间都花在哪里了。
gethostbyaddr
可能必须转到远程 DNS 计算机才能将 IP 地址解析为主机名。如果您的网络设置不佳,或者包含该地址的 DNS 服务器位于西藏山区的偏远地区,则解析将需要一些时间。
从命令行输入:(
其中
xxxx
是您感兴趣的 IP 地址)并查看需要多长时间。根据您在下面标尺线之间的评论:
我正在仅使用 3 台机器的 LAN 上工作。此外,该网络未连接到互联网。 是瞬时的,需要 16 秒(+/- 一些毫秒)
仅行: while:
。以下是 Ping(即时输出)和 nslookup 的输出:
我认为您的问题在于超时。看来您的网络设置可以进行 DNS 名称解析,但不能进行 IP 反向解析。
当您只需输入
nslookup
时,它应该会向您显示它正在尝试使用的 DNS 服务器,这可能会给您提供线索。将名称解析为 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:
(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:
while:
is instantaneous. Below is the output of Ping (instant output) and nslookup:
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.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.
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.