简体   繁体   English

连接到有效IP端口时抛出UnknownHostException

[英]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: 我的建议是:

  • If you are going to use an IP address, you should make sure the InetSocketAddress receives a java.net.InetAddress object. 如果要使用IP地址,则应确保InetSocketAddress接收到java.net.InetAddress对象。 If you look at this API page, you will see there are 2 properties that can modify the behavior of the host name resolution caching. 如果您查看此API页面,将会看到有2个属性可以修改主机名解析缓存的行为。
  • If you are going to have the ip/host name mappings in the server an alternative would be to edit your /etc/hosts file to contain the mappings. 如果要在服务器中具有ip /主机名映射,则另一种方法是编辑/ etc / hosts文件以包含映射。

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.

相关问题 连接到SQL Server时端口号无效 - Port number is not valid while connecting to sql server UnknownHostException:连接到数据库服务器时 - UnknownHostException: while connecting to database server 从 Java 代码连接到 Azure Cosmos DB 时出现 UnknownHostException - UnknownHostException while connecting to Azure Cosmos DB from Java code 连接到 RDS 通过 VPC 对等时 Spring Boot UnknownHostException - Spring Boot UnknownHostException while connecting to RDS throgh VPC peering IP地址上的UnknownHostException - UnknownHostException on an IP Address 是否可以在不连接IP端口的情况下检查IP端口是否打开? - Can I check if an IP port is open or not without connecting to the port? 尝试ping时,抛出java.net.UnknownHostException。 我不明白原因 - While trying to ping, java.net.UnknownHostException gets thrown. I do not understand the reason 错误:使用 Java => java.net.UnknownHostException 连接 Google Cloud Storage 时:oauth2.googleapis.com - Error: while connecting Google Cloud Storage using Java => java.net.UnknownHostException: oauth2.googleapis.com 连接到sql server时端口号不正确 - Port number is not correct while connecting to sql server 使用Java连接hbase时不是host:port - Not a host:port while connecting hbase using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM