HOWTO · Java
How to Fix Java.Net.SocketTimeoutException: Connection Timed Out
In today's article, we will discuss java.net.SocketTimeoutException: Connection timed out.
On this page
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.
- The server is operating fine. However, the
timeoutvalue is set for a shorter time. Therefore, increase the value of thetimeout. - On the remote host, the specified port is not being listened to by any services.
- There is no route to the remote host being sent.
- The remote host does not appear to be allowing any connections.
- There is a problem reaching the remote host.
- 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.
-
On the client side, the first thing we’ll do is construct a null
socket. Following that, we will use theconnect()method and then configure thetimeoutparameter where the timeout should be larger than 0 milliseconds.If the timeout expires before the function returns,
SocketTimeoutExceptionis thrown.Socket s = new Socket(); SocketAddress sAdres = new InetSocketAddress(host, port); s.connect(sAdres, 50000); -
If you want to set a
timeoutvalue from the server side, you can use thesetSoTimeout()function. The value of thetimeoutparameter determines the length of time that theServerSocket.accept()function will block.ServerSocket servers = new new ServerSocket(port); servers.setSoTimeout(10000);Similarly, the
timeoutshould be more than 0 milliseconds. If thetimeoutexpires before the method returns, the method will generate aSocketTimeoutException. -
Determining a connection timeout and then handling it afterward using a
try-catchblock is yet another excellent technique to deal withHttpException.HttpUrlConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(8000);