Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a "Connection Refused" exception and a timeout in HttpClient

I'm trying to determine whether a device on a network is running a web server or not using Java. To do this I'm using Apache's HttpClient. I send a request and if the device runs a web server I get a response - pretty simple.

However, if a web server is not running then I've observed 2 things happen:

  • The request will be ignored and a timeout will occur
  • An exception will be thrown. In particular, it will look like this:

    org.apache.http.conn.HttpHostConnectException: Connect to 192.168.12.133:80 [/192.168.12.133] failed: Connection refused: connect

Is there anything that can be inferred about the device that I'm trying to connect to when I receive the "Connection refused" exception vs. getting no response at all?

Edit:

Just to clarify: What I'm doing is running this against DEVICES on a local network. That means NAS boxes, routers, Windows computers, iPads, etc. Some will have embedded web servers, others will not. Most will not have firewalls.

I also know with 100% certainty that the device is up and running.

like image 710
Sander Smith Avatar asked Sep 26 '17 22:09

Sander Smith


1 Answers

You cannot infer anything with 100% certainty, but:

  • Connection timeout probably means either that the host / port is firewalled, OR the host is "off".
  • Connection refused probably means that the host is not running any service on the port you are trying to connect to.
like image 130
Stephen C Avatar answered Sep 28 '22 06:09

Stephen C