java.net.ConnectException is thrown when client socket is not able to connect to the server socket. java.net.ConnectException is subclass of java.net.SocketException. In this short tutorial, I am going to write a simple program for client socket that sends message to the server socket. Then server socket process the message and prints it. When there is a issue in client for connecting to the server socket, java.net.ConnectException will be thrown. Please read our previous article on WebSocket Support in Java EE 7.
Following are the most common scenarios when this exception is thrown:
- When server is not started and client is trying to connect to the server
- When client is trying to connect to the wrong port number
- When client is trying to connect to the wrong host name or IP address
- When server and client is located remotely. But, they are in the different network where connection is not allowed.
java.net.ConnectException Example
Here is the simple example that demonstrates the ConnectException.
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.net.UnknownHostException; public class ConnectExceptionExample { public static void main(String[] args) { new Thread(new ExampleServer()).start(); new Thread(new ExampleClient()).start(); } } class ExampleServer implements Runnable { @Override public void run() { ServerSocket serverSocket = null; while (true) { try { serverSocket = new ServerSocket(1111); Socket clientSocket = serverSocket.accept(); BufferedReader inputReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); System.out.println("Message Received :" + inputReader.readLine()); } catch (IOException e) { e.printStackTrace(); } finally { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } } } class ExampleClient implements Runnable { @Override public void run() { Socket socket = null; try { Thread.sleep(1000); socket = new Socket("localhost", 1111); PrintWriter outWriter = new PrintWriter(socket.getOutputStream(), true); outWriter.println("Message from Client!"); } catch (InterruptedException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }
When you run the above program, you will get the following output:
Message Received :Message from Client!
Now, change the port number to something like this for server and then run the application.
serverSocket = new ServerSocket(1112);
You will get the java.net.ConnectException as below:
java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:69) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:337) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:198) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:180) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:157) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391) at java.net.Socket.connect(Socket.java:579) at java.net.Socket.connect(Socket.java:528) at java.net.Socket.<init>(Socket.java:425) at java.net.Socket.<init>(Socket.java:208) at infy.org.ExampleClient.run(ConnectExceptionExample.java:47) at java.lang.Thread.run(Thread.java:722) Exception in thread "Thread-1" java.lang.NullPointerException at infy.org.ExampleClient.run(ConnectExceptionExample.java:59) at java.lang.Thread.run(Thread.java:722)
I hope this example will help you to understand when java.net.ConnectException is thrown from your application. If you have any questions, please write it in the comments section.