gpt4 book ai didi

java - HttpExchange.getRemoteAddress() 需要很长时间

转载 作者:行者123 更新时间:2023-12-02 09:58:27 24 4
gpt4 key购买 nike

我正在创建在 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() 被替换为硬编码字符串(“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

编辑:我的失败是没有正确阅读 getCanonicalHostName() 文档。上面写得很清楚

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.

替换为getHostAddress()

最佳答案

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

关于java - HttpExchange.getRemoteAddress() 需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com