简体   繁体   English

HttpExchange.getRemoteAddress()需要很长时间

[英]HttpExchange.getRemoteAddress() takes long time

I am creating server running on Java HttpServer class, everything works fine but I've noticed that call to exchange.getRemoteAddress().getAddress().getCanonicalHostName() takes about 4000 ms, however reading request body is fast even if I remove getRemoteAddress(). 我正在创建在Java HttpServer类上运行的服务器,一切正常,但是我注意到对exchange.getRemoteAddress().getAddress().getCanonicalHostName()调用大约需要4000毫秒,但是即使删除getRemoteAddress,读取请求的主体也很快()。 Does anyone know why HttpExchange.getRemoteAddress() takes so long time? 有谁知道为什么HttpExchange.getRemoteAddress()需要这么长时间? Log from server: 来自服务器的日志:

[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

Log when exchange.getRemoteAddress().getAddress().getCanonicalHostName() is replaced with hardcoded string ("test"): 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

EDIT: My failure, didn't read getCanonicalHostName() documentation properly. 编辑:我的失败,没有正确阅读getCanonicalHostName()文档。 It clearly says 它清楚地说

Gets the fully qualified domain name for this IP address. 获取此IP地址的标准域名。 Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration. 尽力而为方法,这意味着我们可能无法根据基础系统配置返回FQDN。

replaced with getHostAddress() 替换为getHostAddress()

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

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

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