[Solved] java.net.ConnectException: Connection refused

Java is famous for networking applications. Java programmers have written socket programming for client server-based architecture. Most of the socket programming uses TCP-IP protocol for communication between client-server. Remote Method Invocation (RMI) also uses the TCP-IP protocol for communication. Sometime, it will generate java.net.ConnectException Connection refused exception for some communication channel error.

Read Also: Reached end of file while parsing error

In this article, we will describe why this communication channel exception occurs in the first part, and in the second part, we will explain how to solve it.

1. Reasons for java.net.ConnectException

1. PORT or IP is incorrect: If PORT or IP is wrong, then the client will not be able to connect to the desired server. Then it will get this kind of exception.

2. Server is stopped: Suppose a server administration gave some IP address and PORT to you to access the server. But he stopped the server, but your client programs trying to access the server with administrator provided credentials, the client program will get the exception.

3. Protocol mismatch: We know that HTTP, RMI, Websocket, etc. uses TCP as the underlying protocol. HTTP request will start with http:// and RMI protocol starts with rmi// or WebSocket protocol start with ws://. If we request an HTTP request with rmi protocol, it will throw the java.net.ConnectException.

4. Server running in the different port: If server administrator provides you an IP and PORT but server running in a different port, it will give java.net.ConnectException Connection refused.

5. Server or client is not in the network: If the client or server is disconnected from the network, then the client will not be able to find out the server. When the client program will not be able to find out the server, we will get this exception.

6. Firewall Restriction: In the networking world, many malware or virus programs can affect the network or personal computer that is connected to the public network. The different organizations put a firewall to the network to prevent this unwanted attack of various harmful products. Some companies allow internal networks only. If your server programs run on this network and the client IP is not allowed in this network, it will get the exception java.net.ConnectException Connection refused.

2. How to Solve this Exception:

The client can connect with the server easily when

a. The server is running in the IP and PORT provided by the server administrator.
b. There is no restriction on the network.
c. The client and server are on the network.

We have to check the above things first before connecting to the server. We can test this in different ways.

1. We can check the IP and PORT are available for the client by using telnet.

For example, you can use this command to check IP and PORT are running or not.

telnet javahungry.blogspot.com 80


2. We can check the IP is available for the client by pinging IP provided by the server administrator.
For example, you can ping with this command

ping javahungry.blogspot.com

You will get below result:

PING blogspot.l.googleusercontent.com (172.217.163.129): 56 data bytes
64 bytes from 172.217.163.129: icmp_seq=0 ttl=50 time=68.616 ms
64 bytes from 172.217.163.129: icmp_seq=1 ttl=50 time=66.957 ms
64 bytes from 172.217.163.129: icmp_seq=2 ttl=50 time=399.596 ms

3. Practical Example:

1. When we try to connect a database server like MYSQL or Oracle, but IP or PORT is not running or not accessible, then we get the exception java.net.ConnectException.
2. When a mail server is not running the desired PORT or IP, we will get this kind of exception.
3. When an application running in a different PORT or IP, but the client tries to connect, we will get this exception.

Example with Java Code

import java.net.*;
import java.io.*;

public class JavaHungry {

    public static void main(String[] args) {
      //Hostname is defined here
      String hostname = "127.0.0.1";
      //PORT is defined here
      int port = 13;
      // If IP and PORT is invalid it will get exception
      try (Socket socket = new Socket(hostname, port)) {
         // InputStream to read data from socket
         InputStream inputStream = socket.getInputStream();
         InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

         int data;
         StringBuilder outputString = new StringBuilder();
         // Data read from input stream
         while ((data = inputStreamReader.read()) != -1) {
            outputString.append((char) data);
         }
      } catch (IOException ex) {
         // Exception will happen when scoket will not reachable
         System.out.println("Connection Refused Exception: " + ex);
      }
   }
}


If we execute the following command from the command line, we will get the desired exception

javac JavaHungry.java

java JavaHungry

Output:

Connection Refused Exception: java.net.ConnectException: Connection refused (Connection refused)

4. Conclusion:

java.net.ConnectException is the general exception for invalid IP and PORT. We have to concern mainly with the valid IP and PORT and server status. If the server is running with the desired IP or PORT in the proper network, then a client can easily overcome this kind of exception.

About The Author

Subham Mittal has worked in Oracle for 3 years .
For more java articles ,Click here to Subscribe JavaHungry