How to Fix Java.Net.SocketTimeoutException: Connection Timed Out
- Sockets in Java
- Timeouts in Java
-
Causes of
java.net.SocketTimeoutException: Connection timed out
in Java -
Solution to
java.net.SocketTimeoutException: Connection timed out
in Java
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
timeout
value 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 thetimeout
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);
-
If you want to set a
timeout
value from the server side, you can use thesetSoTimeout()
function. The value of thetimeout
parameter determines the length of time that theServerSocket.accept()
function will block.ServerSocket servers = new new ServerSocket(port); servers.setSoTimeout(10000);
Similarly, the
timeout
should be more than 0 milliseconds. If thetimeout
expires before the method returns, the method will generate aSocketTimeoutException
. -
Determining a connection timeout and then handling it afterward using a
try-catch
block is yet another excellent technique to deal withHttpException
.HttpUrlConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(8000);
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.
LinkedInRelated Article - Java Error
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix the Missing Server JVM Error in Java
- How to Fix the 'No Java Virtual Machine Was Found' Error in Eclipse
- How to Fix Javax.Net.SSL.SSLHandShakeException: Remote Host Closed Connection During Handshake
- How to Fix the Error: Failed to Create the Java Virtual Machine
- How to Fix Java.Lang.VerifyError: Bad Type on Operand Stack