Home › Java › java.net.SocketTimeoutException: Read timed out vs connect timed out (Java Fix)
Intermediate 3 min · November 15, 2026

java.net.SocketTimeoutException: Read timed out vs connect timed out (Java Fix)

'Read timed out' means you connected but the reply took longer than your read timeout; 'connect timed out' means nothing answered the connection at all.

N
Naren Founder & Principal Engineer

20+ years shipping production Java in banking & fintech. Drawn from code that ran under real load.

Follow
✓ Production
production tested
November 15, 2026
last updated
2,132
articles · all by Naren
Before you start⏱ 6 min
  • ✓Basic Java
  • ✓Basic TCP/HTTP concepts
 ● Production Incident 🔎 Debug Guide
⚡Quick Answer
  • connect timed out: no reply to the connection attempt (firewall drop, wrong IP, host down).
  • Read timed out: connected, but the reply was slower than the read timeout.
  • The server may have finished the work after you gave up.
  • HttpURLConnection defaults are 0 = wait forever; always set both.
  • setConnectTimeout(3000) and setReadTimeout(...) per endpoint.
  • java.net.http.HttpClient throws HttpConnectTimeoutException / HttpTimeoutException instead.
  • Retry only idempotent requests; make slow work async.
✦ Definition~90s read
What is java.net.SocketTimeoutException?

java.net.SocketTimeoutException is thrown when a socket operation waits longer than its timeout. 'connect timed out' means the TCP connection was never established within the connect timeout. 'Read timed out' means the connection was established, but no data arrived within the read (SO_TIMEOUT) timeout.

★
Connect timed out: you rang and nobody picked up.
Plain-English First

Connect timed out: you rang and nobody picked up. Read timed out: they picked up, you asked a question, and you hung up before they finished answering.

At month end every report request failed after exactly 5 seconds with Read timed out, while the server logged 200 OK in 8 seconds. The client simply stopped waiting.

Two Timeouts, Two Meanings

The message tells you which phase timed out. Connect covers the TCP handshake; read covers waiting for data after the request is sent.

messages.txtTEXT
1
2
java.net.SocketTimeoutException: connect timed out   -> never connected
java.net.SocketTimeoutException: Read timed out      -> connected, reply too slow
🎯 Key Takeaway
Read the message carefully.

Set Both Explicitly (HttpURLConnection)

The defaults are 0, which means wait forever. One slow server can tie up every thread.

Client.javaJAVA
1
2
3
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(3_000);   // fail fast if unreachable
conn.setReadTimeout(15_000);     // fits this endpoint
🎯 Key Takeaway
0 = infinite.

java.net.http.HttpClient

The newer client separates the two too, but throws HttpConnectTimeoutException and HttpTimeoutException.

Client11.javaJAVA
1
2
3
4
HttpClient client = HttpClient.newBuilder()
    .connectTimeout(Duration.ofSeconds(3)).build();
HttpRequest req = HttpRequest.newBuilder(uri)
    .timeout(Duration.ofSeconds(15)).build();
🎯 Key Takeaway
Same idea, different exception.

Retry Carefully

A read timeout doesn't tell you the server failed; it may have completed. Retry GETs and other idempotent calls; protect writes with idempotency keys.

retry.txtTEXT
1
2
GET  /report        -> safe to retry
POST /payment       -> may already have happened: use an idempotency key
🔥Slow by design?
If an endpoint is legitimately slow, make it asynchronous: submit the job, then poll for the result.
🎯 Key Takeaway
Timeouts aren't failures on the server.
● Production incidentPOST-MORTEMseverity: high

The Reports That Finished After the Client Left

Symptom
Every month-end report call failed after exactly 5.0 s with SocketTimeoutException: Read timed out.
Assumption
The team assumed the report service was down.
Root cause
A shared HTTP client used readTimeout=5000 for every endpoint. Month-end reports took about 8 seconds.
Fix
Gave the report endpoint its own 15-second read timeout and moved the largest reports to submit-then-poll.
Key lesson
  • Check which timeout fired.
  • Set timeouts per endpoint.
  • The server may have finished anyway.
Production debug guideWhich one fired, and why?3 entries
Symptom · 01
Fails at exactly N seconds
→
Fix
That's your configured timeout; compare with the server's response time.
Symptom · 02
connect timed out
→
Fix
Check the IP and firewall: nc -vz -w 3 host port.
Symptom · 03
Threads hang for hours
→
Fix
No timeout set: the default may be infinite.
⚙ Quick Reference
4 commands from this guide
FileCommand / CodePurpose
messages.txtjava.net.SocketTimeoutException: connect timed out -> never connectedTwo Timeouts, Two Meanings
Client.javaHttpURLConnection conn = (HttpURLConnection) url.openConnection();Set Both Explicitly (HttpURLConnection)
Client11.javaHttpClient client = HttpClient.newBuilder()java.net.http.HttpClient
retry.txtGET /report -> safe to retryRetry Carefully

Key takeaways

1
connect timed out = no answer.
2
Read timed out = reply too slow.
3
Defaults can be infinite.
4
Set timeouts per endpoint.
5
Retry only safe calls.

Common mistakes to avoid

3 patterns
×

No timeouts

Symptom
Threads hang for hours.
Fix
Set connect and read timeouts.
×

One read timeout for every endpoint

Symptom
Slow endpoints always fail.
Fix
Per-endpoint values.
×

Blind retries of POSTs

Symptom
Duplicate orders or payments.
Fix
Idempotency keys.
INTERVIEW PREP · PRACTICE MODE

Interview Questions on This Topic

Q01SENIOR
Difference between connect timeout and read timeout?
Q01 of 01SENIOR

Difference between connect timeout and read timeout?

ANSWER
Connect timeout limits establishing the TCP connection; read timeout limits waiting for data on an established connection.
FAQ · 3 QUESTIONS

Frequently Asked Questions

01
What does java.net.SocketTimeoutException: Read timed out mean?
02
How do I fix connect timed out?
03
Should I just increase the timeout?
N
Naren Founder & Principal Engineer

20+ years shipping production Java in banking & fintech. Drawn from code that ran under real load.

Follow
✓ Verified
production tested
November 15, 2026
last updated
2,132
articles · all by Naren
🔥

That's Exceptions. Mark it forged?

3 min read · try the examples if you haven't

←
Previous
java.net.ConnectException: Connection Refused
11 / 11 · Exceptions
Next
ADB Device Unauthorized Fix
→