Handling java.net.ConnectException – 处理java.net.ConnectException

最后修改: 2020年 4月 17日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this quick tutorial, we’ll discuss some possible causes of java.net.ConnectException. Then, we’ll show how to check the connection with the help of two publicly available commands and a small Java example.

在这个快速教程中,我们将讨论java.net.ConnectException的一些可能原因。然后,我们将展示如何在两个公开的命令和一个小的Java例子的帮助下检查连接情况。

2. What Causes java.net.ConnectException

2.什么原因导致java.net.ConnectException

The java.net.ConnectException exception is one of the most common Java exceptions related to networking. We may encounter it when we’re establishing a TCP connection from a client application to a server. As it’s a checked exception, we should handle it properly in our code in a try-catch block.

java.net.ConnectException异常是与网络有关的最常见的Java异常之一。当我们建立一个从客户端应用程序到服务器的TCP连接时,我们可能会遇到它。由于它是一个被检查的异常,我们应该在代码中用try-catch块正确处理它。

There are many possible causes of this exception:

造成这种异常的原因有很多。

  • The server we are trying to connect to is simply not started, therefore, we can’t obtain a connection
  • The host and port combination we are using to connect to the server might be incorrect
  • A firewall might block connections from specific IP addresses or ports

3. Catching the Exception Programmatically

3.通过程序捕捉异常

We usually connect to the server programmatically using the java.net.Socket class. To establish a TCP connection, we must make sure we’re connecting to the correct host and port combination:

我们通常使用java.net.Socket类以编程方式连接到服务器。要建立一个TCP连接,我们必须确保我们连接到正确的主机和端口组合:

String host = "localhost";
int port = 5000;

try {
    Socket clientSocket = new Socket(host, port);

    // do something with the successfully opened socket

    clientSocket.close();
} catch (ConnectException e) {
    // host and port combination not valid
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

If the host and port combination is incorrect, Socket will throw a java.net.ConnectException. In our code example above, we print the stack trace to better determine what’s going wrong. No visible stack trace means that our code successfully connected to the server.

如果主机和端口组合不正确,Socket将抛出一个 java.net.ConnectException.在我们上面的代码示例中,我们打印了堆栈跟踪以更好地确定出错的地方。没有可见的堆栈跟踪意味着我们的代码成功连接到了服务器。

In this case, we should check the connection details before proceeding. We should also check that our firewall isn’t preventing the connection.

在这种情况下,我们应该在继续之前检查连接的细节。我们还应该检查我们的防火墙是否阻止了该连接。

4. Checking Connections With the CLI

4.使用 CLI 检查连接情况</b

Through the CLI, we can use the ping command to check whether the server we’re trying to connect to is running.

通过CLI,我们可以使用ping命令来检查我们试图连接的服务器是否正在运行

For example, we can check if the Baeldung server is started:

例如,我们可以检查Baeldung服务器是否已经启动。

ping baeldung.com

If the Baeldung server is running, we should see information about sent and received packages.

如果Baeldung服务器正在运行,我们应该看到关于发送和接收包裹的信息。

PING baeldung.com (104.18.63.78): 56 data bytes
64 bytes from 104.18.63.78: icmp_seq=0 ttl=57 time=7.648 ms
64 bytes from 104.18.63.78: icmp_seq=1 ttl=57 time=14.493 ms

telnet is another useful CLI tool that we can use to connect to a specified host or IP address. Additionally, we can also pass an exact port that we want to test. Otherwise, default port 23 will be used.

telnet是另一个有用的CLI工具,我们可以用它来连接到一个指定的主机或IP地址。此外,我们还可以传递一个我们想要测试的确切端口。否则,将使用默认端口23。

For example, if we want to connect to the Baeldung website on port 80, we can run:

例如,如果我们想连接到80端口的Baeldung网站,我们可以运行。

telnet baeldung.com 80

It’s worth noting that ping and telnet commands may not always work — even if the server we’re trying to reach is running and we’re using the correct host and port combination. This is often the case in production systems, which are usually heavily secured to prevent unauthorized access. Besides, a firewall blocking specific internet traffic might be another cause of failure.

值得注意的是,pingtelnet命令可能并不总是有效–即使我们试图到达的服务器正在运行,并且我们使用了正确的主机和端口组合。在生产系统中经常出现这种情况,这些系统通常有很强的安全性,以防止未经授权的访问。此外,阻止特定互联网流量的防火墙可能是另一个失败的原因。

5. Conclusion

5.总结

In this quick tutorial, we’ve discussed the common Java network exception, java.net.ConnectException.

在这个快速教程中,我们已经讨论了常见的Java网络异常,java.net.ConnectException

We started with an explanation of possible causes of that exception. We showed how to catch the exception programmatically, along with two useful CLI commands that might help to determine the cause.

我们首先解释了导致该异常的可能原因。我们展示了如何以编程方式捕获该异常,以及两个有用的CLI命令,它们可能有助于确定原因。

As always, the code shown in this article is available over on GitHub.

一如既往,本文中显示的代码可在GitHub上获得