java的netconnection,java.net.ConnectException: Connection refused: connect

案例:使用域名访问服务器,由于服务器的域名对应于多个ip,其中有一个ip没监听8080端口,客户端访问时只是随机取其一访问,导致不能每次都访问成功,经常报Connection refused

现在服务器把两个ip都开启8080端口,就没问题了

一般来说,是访问服务器的参数写得不对。参数不对有两种可能:a、客户端写错了 b、服务器没在监听这个端口!

异常打印:

Caused by: java.net.ConnectException: failed to connect to xxx.xxx.x.x  after 3000ms: isConnected failed: ECONNREFUSED (Connection refused)

at libcore.io.IoBridge.isConnected(IoBridge.java:273)

at libcore.io.IoBridge.connectErrno(IoBridge.java:188)

at libcore.io.IoBridge.connect(IoBridge.java:130)

at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:129)

at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:356)

at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)

at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)

at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:356)

at java.net.Socket.connect(Socket.java:616)

参考文章: https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html#axzz5KBHV4ZRR

https://www.cnblogs.com/cheerfulness/p/BUG_4.html

可能原因:

java.net.ConnectException: Connection refused Error – Possible reasons

1) Client and Server, either or both of them are not in the network.

Yes it's possible that they are not connected to LAN or internet or any other network, in that case, Java will throw

"java.net.ConnectException: Connection refused" exception on client side.

Read more: https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html#ixzz5KBIOc300

2) Server is not running

The second most common reason is the server is down and not running. In that case, also you will get java.net.ConnectException: Connection refused error.

What I don't like is that the message it gives, no matter what is the reason it prints the same error. By the way, you can use following networking commands

e.g. ping to check if the server is running and listening on the port.

Read more: https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html#ixzz5KBISKVQ2

3) The server is running but not listening on the port, a client is trying to connect.

This is another common cause of "java.net.ConnectException: Connection refused", where the server is running but listening on the different port.

It’s hard to figure out this case, until, you think about it and verify the configuration. If you

are working on a large project and have a hierarchical configuration file, Its possible that either default configuration

is taking place or some other settings are overriding your correct setting.

Read more: https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html#ixzz5KBIY54E4

4) Firewall is not permitted for host-port combination

Almost every corporate network is protected by firewalls. If you are connecting to some other companies network e.g.

opening an FIX session to the broker, in any Electronic Trading System, then you need to raise firewall

request from both sides to ensure that they permit each other's IP address and port number. If the firewall is not allowing

connection then also you will receive same java.net.ConnectException: Connection refused exception in Java application.

Read more: https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html#ixzz5KBIcIZwr

5) Host Port combination is incorrect.

This could be another reason of java.net.ConnectException: Connection refused: connect.It’s quite possible that either you are

providing incorrect host port combination or earlier host port combination has been changed on the server side. Check the

latest configuration on both client and server side to avoid connection refused exception.

Read more: https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html#ixzz5KBIoFxPp

6) Incorrect protocol in Connection String

TCP is underlying protocol for much high-level protocol including HTTP, RMI and others. While passing connection

string, you need to ensure that you are passing correct protocol, which server is expecting e.g. if server has exposed

its service via RMI than connection string should begin with rmi://

Read more: https://javarevisited.blogspot.com/2013/02/java-net-ConnectException-Connection-refused.html#ixzz5KBIx9qa1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值