Friday, February 8, 2013

How to Fix java.net.ConnectException: Connection refused: connect in Java

java.net.ConnectException: Connection refused: connect is one of the most common networking exception in Java. This error comes when you are working with client-server architecture and trying to make TCP connection from client to server. Though this is not as cryptic as java.lang.OutOfMemoryError: Java heap Space or java.lang.UnsupportedClassVersionError, it’s still a frequent problem in distributed Java applications. java.net.ConnectException: Connection refused: connect also comes in case of RMI (Remote Method Invocation), because RMI also use TCP IP protocol underneath. While writing client socket code in Java, You should always provide proper handling of this exception. In this Java tutorial, we will see why connection refused exception comes and how to solve java.net.ConnectException: Connection refused: connect Exception in Java.

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

java.net.ConnectException: Connection refused: connect solution fixConnection refused is a clear case of client trying to connect on a TCP port but not able to succeed. Here are some of the possible reason why java.net.ConnectException: Connection refused: connect comes:

1) Client and Server, either or both of them are not in network.
Yes its 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.

2) Server is not running
Second most common reason is 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 print same error. By the way you can use following networking commands e.g. ping to check if server is running and listening on port.

3) Server is running but not listening on port, client is trying to connect.
This is another common cause of "java.net.ConnectException: Connection refused", where server is running but listening on different port. It’s hard to figure out this case, until, you think about it and verify configuration. If you
are working on large project and have hierarchical configuration file, Its possible that either default configuration
is taking place or some other settings is overriding your correct setting.

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 a FIX session to broker, in any Electronic Trading System, than you need to raise firewall
request from both side to ensure that they permit each others IP address and port number. If firewall is not allowing
connection than also you will receive same java.net.ConnectException: Connection refused exception in Java application.

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 server side. Check latest configuration on both client and server side to avoid connection refused exception.

6) Incorrect protocol in Connection String
TCP is underlying protocol for many 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://

How to solve java.net.ConnectException: Connection refused

Simple solution is to find out the actual reason of java.net.ConnectException: Connection refused, Possibly one of the reasons mentioned above and solve that. What is most important here is approach of finding correct cause and solution. Here are some tips which may help you to identify real cause of  java.net.ConnectException: Connection refused :

1) First try to ping destination host , if host is ping-able it means client and server machine are in network.
2) Try connecting to server host and port using telnet. If you are able to connect means something is wrong with your client code. Also by observing telnet output you will come to know whether server is running or not or server is disconnecting the connection.

That’s all on What is java.net.ConnectException: Connection refused: connect error in Java and how to fix it. As I said there is no definite reason for this error and could be many things which can go wrong. Thankfully, this is more to do with configuration or environment and once you find out real cause, you might not even need to make any change on your side.

Related Java debugging tutorials for Java programmers

4 comments :

Mohanrajurallabandi said...

at com.mysql.jdbc.NonRegisteringDriver.connect ...why we get this exception and how can we resolve it..can anyone tell me please

Anonymous said...

java.net.ConnectException: Connection refused: connect , is pretty general error. Real error is always behind, I suggest looking top level error and caused by clause. This will give you more ideas. In simple words, this error just says that your client and server is not able to connect, but doesn't tell anything about why connection is not happening. There could be thousands of reason of a client and server not connecting including environment issues, host settings, network settings and application settings itself.

Anonymous said...

Hello there, I am getting following Connection refused: connect exception while trying to send an email from localhost. I am using smpt port 25

com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1984)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:656)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at Testing.main(Testing.java:62)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:301)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1950)
... 7 more
Please suggest what is wrong?

Gaurav Singh said...

I am also facing this type of problem.......please suggest me
Exception in thread "main" org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1227)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:322)
at javax.mail.Service.connect(Service.java:236)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:306)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:296)
at com.aa.meals.mail.MealsMailSenderBean.sendMail(MealsMailSenderBean.java:21)
at com.aa.common.log.TestMail.main(TestMail.java:14)

Post a Comment