[英]UnknownHostException is thrown while connecting to a valid ip port
When i try to connect to a socket (valid ip:port) UnknownHostException is thrown randomly! 当我尝试连接到套接字(有效ip:port)时,会随机抛出UnknownHostException!
Socket socket = new Socket();
socket.connect(new InetSocketAddress(ipAddress, port), 1000);
Our local application environment's /etc/resolve.conf is configured with the ipAddress of our local nameserver, with this configuration UnknownHostException occurs randomly (almost in 50:50 ratio). 我们本地应用程序环境的/etc/resolve.conf是使用我们本地名称服务器的ipAddress配置的,这种配置UnknownHostException是随机发生的(几乎以50:50的比例)。
But when the resolve.conf's entries are cleared and left blank UnknownHostException never occurs , and the socket connection is smoothly established. 但是,当resolve.conf的条目被清除并且留空时 , UnknownHostException将永远不会发生 ,并且套接字连接会顺利建立。
How can I be able to resolve this issue as it is not possible to leave the resolve.conf blank also! 由于无法将resolve.conf留空,我该如何解决此问题!
OS: CentOS 7
操作系统:CentOS 7
Usually your resolv.conf file is populated by your DHCP client. 通常,您的resolv.conf文件由DHCP客户端填充。 If you see the content of this file, it may start with
如果您看到此文件的内容,则可能以
; generated by /sbin/dhclient-script
It is not recommended to manually edit this file. 不建议手动编辑该文件。 This file points to a DNS (or multiple DNS servers) and if you are going to use DNS resolution then the correct mapping must exist in the DNS.
该文件指向一个DNS(或多个DNS服务器),如果要使用DNS解析,则DNS中必须存在正确的映射。
In your example you don't mention what type of variable is ipAddress . 在您的示例中,您没有提到ipAddress是什么类型的变量。 The behavior you describe indicates that you ipAddress variable is a string.
您描述的行为表明ipAddress变量是一个字符串。 Which will cause the InetSocketAddress constructor to attempt host name resolution which might cause the behavior you are experiencing.
这将导致InetSocketAddress构造函数尝试解析主机名,这可能会导致您遇到的行为。
My suggestions are: 我的建议是:
From the Java API. 来自Java API。 These are the properties that drive host name resolution caching behavior:
这些是驱动主机名解析缓存行为的属性:
Two Java security properties control the TTL values used for positive and negative host name resolution caching:
两个Java安全属性控制用于正主机名解析和负主机名解析缓存的TTL值:
networkaddress.cache.ttl Indicates the caching policy for successful name lookups from the name service.
networkaddress.cache.ttl指示用于从名称服务成功查找名称的缓存策略。 The value is specified as as integer to indicate the number of seconds to cache the successful lookup.
该值被指定为整数,以指示缓存成功查找的秒数。 The default setting is to cache for an implementation specific period of time.
默认设置是缓存实施特定时间段。 A value of -1 indicates "cache forever".
值-1表示“永远缓存”。
networkaddress.cache.negative.ttl (default: 10) Indicates the caching policy for un-successful name lookups from the name service.
networkaddress.cache.negative.ttl (默认值:10)指示从名称服务中查找不成功的名称的缓存策略。 The value is specified as as integer to indicate the number of seconds to cache the failure for un-successful lookups.
该值被指定为整数,以指示为不成功的查询而缓存故障的秒数。 A value of 0 indicates "never cache".
值为0表示“从不缓存”。 A value of -1 indicates "cache forever".
值-1表示“永远缓存”。
Hope this helps. 希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.