简体   繁体   中英

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(). Does anyone know why HttpExchange.getRemoteAddress() takes so long time? 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"):

[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. It clearly says

Gets the fully qualified domain name for this IP address. Best effort method, meaning we may not be able to return the FQDN depending on the underlying system configuration.

replaced with getHostAddress()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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