繁体   English   中英

HttpExchange.getRemoteAddress()需要很长时间

[英]HttpExchange.getRemoteAddress() takes long time

我正在创建在Java HttpServer类上运行的服务器,一切正常,但是我注意到对exchange.getRemoteAddress().getAddress().getCanonicalHostName()调用大约需要4000毫秒,但是即使删除getRemoteAddress,读取请求的主体也很快()。 有谁知道为什么HttpExchange.getRemoteAddress()需要这么长时间? 来自服务器的日志:

[pool-1-thread-1 | 13:25:54] Received request from 192.168.0.104 / Obtaining remote address took 4538 ms
[pool-1-thread-1 | 13:25:54] Downloading request body from 192.168.0.104 took 0 ms
[pool-1-thread-1 | 13:25:54] Generating response body from 192.168.0.104 took 6 ms
[pool-1-thread-1 | 13:25:54] Processing lock request from 192.168.0.104

exchange.getRemoteAddress().getAddress().getCanonicalHostName()替换为硬编码字符串(“测试”)时记录:

[pool-1-thread-1 | 13:28:00] Received request from test / Obtaining remote address took 0 ms
[pool-1-thread-1 | 13:28:00] Downloading request body from test took 1 ms
[pool-1-thread-1 | 13:28:00] Generating response body from test took 7 ms
[pool-1-thread-1 | 13:28:00] Processing lock request from test

编辑:我的失败,没有正确阅读getCanonicalHostName()文档。 它清楚地说

获取此IP地址的标准域名。 尽力而为方法,这意味着我们可能无法根据基础系统配置返回FQDN。

替换为getHostAddress()

这是因为查找给定IP地址的主机名通常会涉及反向DNS查找 ,即网络查询(或一系列查询),这是一项昂贵的操作,并且比使用硬编码的字符串值自然要慢得多。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM