How to Fix Java.Net.SocketTimeoutException: Connection Timed Out

Muhammad Zeeshan Feb 02, 2024
  1. Sockets in Java
  2. Timeouts in Java
  3. Causes of java.net.SocketTimeoutException: Connection timed out in Java
  4. Solution to java.net.SocketTimeoutException: Connection timed out in Java
How to Fix Java.Net.SocketTimeoutException: Connection Timed Out

In today’s article, we will discuss java.net.SocketTimeoutException: Connection timed out. But first, let’s take a closer look at the concepts of sockets and timeouts.

Sockets in Java

A logical link between two computer applications might have multiple endpoints, one of which is a socket.

To put it another way, it is a logical interface that applications use to transmit and receive data over a network. An IP address and a port number comprise a socket in its most basic form.

A unique port number is allotted to each socket, which is utilized to identify the service. Connection-based services use stream sockets that are based on TCP.

Because of this, Java offers the java.net.Socket class as a client-side programming option.

On the other hand, the java.net.ServerSocket class is utilized in server-side TCP/IP programming. The datagram socket based on UDP is another kind of socket, and it’s the one that’s employed for connectionless services.

Java supports java.net.DatagramSocket for UDP operations.

Timeouts in Java

An instance of a socket object is created when the socket constructor is called, allowing a connection between the client and the server from the client side.

As input, the constructor expects to receive the address of the remote host and the port number. After that, it tries to use the parameters provided to establish a connection to the remote host.

The operation will prevent other processes from proceeding until a successful connection is created. But, the application will throw the following error if the connection is not successful after a specified time.

java.net.SocketTimeoutException : Connection timed out

Listening to incoming connection requests, the ServerSocket class on the server side is permanently active. When a connection request is received by ServerSocket, the accept function is invoked to create a new socket object.

Similar to the previous method, this one blocks until the remote client is connected.

Causes of java.net.SocketTimeoutException: Connection timed out in Java

The following are some possible reasons for the error.

  1. The server is operating fine. However, the timeout value is set for a shorter time. Therefore, increase the value of the timeout.
  2. On the remote host, the specified port is not being listened to by any services.
  3. There is no route to the remote host being sent.
  4. The remote host does not appear to be allowing any connections.
  5. There is a problem reaching the remote host.
  6. Internet connection that is either slow or unavailable.

Solution to java.net.SocketTimeoutException: Connection timed out in Java

We can pre-set the timeout option for client and server activities. Adding the try and catch constructs would be an appropriate solution.

  1. On the client side, the first thing we’ll do is construct a null socket. Following that, we will use the connect() method and then configure the timeout parameter where the timeout should be larger than 0 milliseconds.

    If the timeout expires before the function returns, SocketTimeoutException is thrown.

    Socket s = new Socket();
    SocketAddress sAdres = new InetSocketAddress(host, port);
    s.connect(sAdres, 50000);
    
  2. If you want to set a timeout value from the server side, you can use the setSoTimeout() function. The value of the timeout parameter determines the length of time that the ServerSocket.accept() function will block.

    ServerSocket servers = new new ServerSocket(port);
    servers.setSoTimeout(10000);
    

    Similarly, the timeout should be more than 0 milliseconds. If the timeout expires before the method returns, the method will generate a SocketTimeoutException.

  3. Determining a connection timeout and then handling it afterward using a try-catch block is yet another excellent technique to deal with HttpException.

    HttpUrlConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(8000);
    
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Java Error