11

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.

Sander Smith
  • 1,371
  • 4
  • 20
  • 30

1 Answers1

7

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.

Other possibilities to consider include:

  • The possibility of network or virtual network misconfiguration causing network packets going to the wrong place or being dropped.

  • The possibility of hardware errors or "the cable fell out" or ...


Having said that, by current (2023) StackOverflow standards this question is off-topic. You should ask questions about networking and diagnosing network error on ServerFault. This is not a programming question.

Here's a related question on ServerFault:

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216