查阅资料,在外网上即一篇解决方法
3. How to solve Connect Exception
Following the directives of the above paragraphs, there are one or two things that you can check when you come across a java.net.ConnectException
.
- Check the client configuration: That involves checking whether the client is trying to connect to the correct IP address and the correct port. For more complex connection, for example if you are trying to make an RMI (Remote Method Invocation) call, you should check if you are using the correct protocol, port and IP in the connection String. Same goes if you’re trying to connect to a MySQL server via JDBC.
- Check the server configuration: That involves checking whether the server can actually listen to the port that the client tries to connect to.
- Make sure that the server is up and running: Although all your configurations are fine, your server application might have crushed and stopped, and therefore it is down.
- Check if the remote client or server are connected to the net: You have to make sure that both machines are accessible in the network you are working on. Whether this is the same host (localhost), or your home network which means that both of the machines have to be connected to the same router (or be accessible by one another anyway), or it is a company network which means that both machines have to have access to he company network, or if its a completely remote connection then both machines have to have Internet access. In sum, make sure that the hosts that run your client and server application are accessible by one another.
- Check firewall configurations: This is a very common source of
java.net.ConnectException
. If you are trying to connect to a remote host that you know is running and you have all the configurations correct but you still get that exceptions, chances are that the remote server host blocks this port with a firewall. For example, it might only allow local connection to that port, but not remote connections. or might only allow specific IPs to connect to the port etc. Furthermore it is highly possible that your own network cannot allow that connection. For example if you stand behind your company network and you try to connect to a remote server, it is possible that your company’s router will block the connection.
总结就是,检查客户端配置,检查服务器端配置,确定服务器端起来并且能运行,检查是否联网,检查防火墙配置。
总结就是五点,希望可以帮助大家!