Why This Happens
Connection refused means no process is listening on the target host and port. The OS rejects the TCP connection immediately. This happens when the server has not started yet, is listening on a different port, or a firewall is blocking the connection.
The Problem
// Server not running on port 8080
Socket socket = new Socket("localhost", 8080); // ConnectExceptionThe Fix
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress("localhost", 8080), 5000);
} catch (ConnectException e) {
System.err.println("Server not available. Ensure it is running on port 8080.");
}Step-by-Step Fix
- 1
Verify server is running
Check that the target server process is running and listening on the expected port using netstat or ss.
- 2
Check host and port
Verify the hostname and port number are correct. Check for typos and ensure DNS resolution works.
- 3
Check firewall and network
Verify that firewalls, security groups, or network policies are not blocking the connection.
Bugsly catches this automatically
Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.
Try Bugsly free