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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,907评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,987评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,298评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,586评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,633评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,488评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,275评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,176评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,619评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,819评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,932评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,655评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,265评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,871评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,994评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,095评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,884评论 2 354

推荐阅读更多精彩内容

  • The Inner Game of Tennis W Timothy Gallwey Jonathan Cape ...
    网事_79a3阅读 12,069评论 3 20
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,331评论 0 10
  • 光看背影就能萌化人的动物除了滚滚还有谁???还有谁???
    无忧之地阅读 673评论 5 2
  • 夏天的天气就是这样调皮,忽晴忽雨,忽冷忽热。看着每天都在进步的自己,对于我来说,雨天亦是晴天,心中有向往,哪里都是...
    9ea8092f54e0阅读 144评论 0 1
  • 春分刚过,清明就到了。这几天和弟弟商量,避过这几天扫墓的高峰,再去给妈妈扫墓,所以清明节这几天便闲在家里。 姐姐从...
    薇薇薛阅读 294评论 2 0