Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish between connection refused and connection timed out

When using java.net.Socket.connect(), both a refused connection and a timeout result in a ConnectException.

java.net.ConnectException: Connection timed out: connect

java.net.ConnectException: Connection refused: connect

How can I safely distinguish between the two? Sure parsing the error message does the job. But when the message changes in a future Java release, I'm out of luck.

The bigger picture: I'm writing a web service client using JAX-WS with a Metro implementation. When a web service call fails, I want to report the reason for the failure clearly so the issue can be resolved quickly.

like image 711
Stijn Van Bael Avatar asked Jul 29 '10 14:07

Stijn Van Bael


People also ask

What does it mean when a connection is refused?

In general, connection refused errors are generated during a connect system call when an application attempts to connect using TCP to a server port which is not open.

What is the difference between connection and read timeout?

The connection timeout is the timeout in making the initial connection; i.e. completing the TCP connection handshake. The read timeout is the timeout on waiting to read data1.

What is the difference between connection timeout and connection request timeout?

request timeout — a time period required to process an HTTP call: from sending a request to receiving a response. connection timeout — a time period in which a client should establish a connection with a server.

What does connection time out mean?

A server connection timeout means that a server is taking too long to reply to a data request made from another device. Timeouts are not a reply message: they show up when there isn't a reply and a server request is not fulfilled in a predetermined length of time.


1 Answers

Measure the time passed between invoking connect() and the exception was thrown, and pass that information on.

like image 190
Thorbjørn Ravn Andersen Avatar answered Sep 21 '22 13:09

Thorbjørn Ravn Andersen