Java.net.ConnectionException
java.net.ConnectException: Connection rejected: the interface is the most continuous sort of happening, organizing special cases in Java at whatever point the product is in client-server engineering and attempting to make a TCP association from the client to the server. We want to deal with the special case cautiously to satisfy the correspondence issue.
The java.net.ConnectException special case is one of the most widely recognized Java exemptions connected with systems administration. We might experience it while we're laying out a TCP association from a client application to a server. As it's a checked exemption, we ought to deal with it appropriately in our code in an attempt to get a block.
Reasons for java.net.ConnectionException: connection refused Exception
- On the off chance that the server isn't running. Generally, ports like 8080 (for tomcat), 3000 or 4200 (for respond/rakish), 3306(MySQL), 27017(MongoDB) or involved by a few different specialists or thoroughly down, for example, an occasion not began.
- Here and there, a server might be running, however, not tuning in on port in light of some superseded settings and so forth.
- Ordinarily, for the sake of security, the Firewall will be there, and in the event that it refuses the correspondence.
- Accidentally, some unacceptable port is referenced in the port or the arbitrary port age number given.
- Association string data wrong: for example, while connecting the database using JDBC, if we give a wrong password, the Exception may rise.
- As client and server included, both ought to be in an organization like LAN or web. It will toss an exemption on the client side.
- The server we are attempting to interface with is basically not begun. In this manner, we can't get an association.
- The host and port mix we are utilizing to interface with the server may be erroneous
- A firewall could hinder associations from explicit IP locations or ports
Example 1:
Example program to show the java.net.ConnectionException Exception:
// program to demonstrate the java.net.Connection Exception
import java.io.*;
import java.net.*;
importjava.util.*;
// class with main
public class HelloWorld {
public static void main(String[] args)
{
// main method
// defining the local hostname and the value of the local host is 127.0.0.1
String host = "127.0.0.1";
// defining the port and the port id must be 8080 or 8000
// the post id should not be 100
// if it is rather than 8080 or 8000 the exception rises
int port = 100;
// Try block to check for exceptions
try (Socket so = new Socket(host, port)) {
// defining an InputStream to read the socket data
InputStream input = so.getInputStream();
InputStreamReaderis = new InputStreamReader(input);
int da;
StringBuilder output= new StringBuilder();
// reading the data from the input stream
while ((da = isr.read())!= -1) {
output.append((char)da);
}
}
// Handling the Exception using catch block
catch (IOException e) {
/* If the given hostname and port number are invalid, the network can't be laid out, and consequently blunder is tossed Exception will happen when the attachment will not be reachable */
System.out.println("Connection Refused Exception as the given hostname and port are invalid : "+ e.getMessage());
}
}
}
Output:

the output is: Java -cp /tmp/9BLwnhzXAr HelloWorld
Connection Refused Exception as the given hostname and port are invalid: Connection refused (Connection refused)
Example 2:
A program to setup database connection
// java program to setup the connection between the Java and database
import java.io.*;
importjava.util.*;
importjava.sql.*; // importing sql package
classHelloWorld
{
public static void main(String args[])
{
// checking the Exception using the try block
try {
// initializing the connection object value as NULL
Connection connect = null;
String dr = "com.MySQL.jdbc.Driver";
// Step 2: loading the driver class
// Here, in the event that IP address isn't your localhost, need to determine that or explicit location
String IP = "local"
// giving the port id as 3306
String urlmysql://IPADDRESS:3306/= "JDBC:mysql://IP:3306/
// followed as DB = MySQL username= Scott, password=tiger
String DB = "<your dbname>";
String dbUser = "<username>";
String dbPasswd = "<password>";
// By using the forName() method, we are loading the driver class
Class.forName(driver).newInstance();
// making the connection using the connection manager
co = DriverManager.getConnection(url1 + DB, dbUser,dbPasswd);
// Displaying the successful message if the connection is set up successful
System.out.println(" The database connected sucessfully");
}
// Handling the Exception using the catch block
catch (IOException e) {
// Exemption will happen when the IPAddress and port number are confounded By pinging. We can address that
System.out.println("Connection Refused Exception as the given hostname and port are invalid : "+ e.getMessage());
}
}
}