What can be the reasons of connection refused errors?

CSocketsConnection Refused

C Problem Overview


I'm trying to write a server program in C, using another client, I get this error when I try to connect through port 2080 for example.

connection refused

What can be the reasons of this error?

C Solutions


Solution 1 - C

There could be many reasons, but the most common are:

  1. The port is not open on the destination machine.

  2. The port is open on the destination machine, but its backlog of pending connections is full.

  3. A firewall between the client and server is blocking access (also check local firewalls).

After checking for firewalls and that the port is open, use telnet to connect to the ip/port to test connectivity. This removes any potential issues from your application.

Solution 2 - C

The error means the OS of the listening socket recognized the inbound connection request but chose to intentionally reject it.

Assuming an intermediate firewall is not getting in the way, there are only two reasons (that I know of) for the OS to reject an inbound connection request. One reason has already been mentioned several times - the listening port being connected to is not open.

There is another reason that has not been mentioned yet - the listening port is actually open and actively being used, but its backlog of queued inbound connection requests has reached its maximum so there is no room available for the inbound connection request to be queued at that moment. The server code has not called accept() enough times yet to finish clearing out available slots for new queue items.

Wait a moment or so and try the connection again. Unfortunately, there is no way to differentiate between "the port is not open at all" and "the port is open but too busy right now". They both use the same generic error code.

Solution 3 - C

If you try to open a TCP connection to another host and see the error "Connection refused," it means that

  1. You sent a TCP SYN packet to the other host.
  2. Then you received a TCP RST packet in reply.

RST is a bit on the TCP packet which indicates that the connection should be reset. Usually it means that the other host has received your connection attempt and is actively refusing your TCP connection, but sometimes an intervening firewall may block your TCP SYN packet and send a TCP RST back to you.

See https://www.rfc-editor.org/rfc/rfc793 page 69:

> SYN-RECEIVED STATE > > If the RST bit is set > > If this connection was initiated with a passive OPEN (i.e., came > from the LISTEN state), then return this connection to LISTEN state > and return. The user need not be informed. If this connection was > initiated with an active OPEN (i.e., came from SYN-SENT state) then > the connection was refused, signal the user "connection refused". In > either case, all segments on the retransmission queue should be > removed. And in the active OPEN case, enter the CLOSED state and > delete the TCB, and return.

Solution 4 - C

Connection refused means that the port you are trying to connect to is not actually open.

So either you are connecting to the wrong IP address, or to the wrong port, or the server is listening on the wrong port, or is not actually running.

A common mistake is not specifying the port number when binding or connecting in network byte order...

Solution 5 - C

Check at the server side that it is listening at the port 2080. First try to confirm it on the server machine by issuing telnet to that port:

telnet localhost 2080

If it is listening, it is able to respond.

Solution 6 - C

1.Check your server status.

2.Check the port status.

For example 3306 netstat -nupl|grep 3306.

3.Check your firewalls. For example add 3306

vim /etc/sysconfig/iptables
# add
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT

Solution 7 - C

Although it does not seem to be the case for your situation, sometimes a connection refused error can also indicate that there is an ip address conflict on your network. You can search for possible ip conflicts by running:

 arp-scan -I eth0 -l | grep <ipaddress>

and

arping <ipaddress>

This AskUbuntu question has some more information also.

Solution 8 - C

I get the same problem with my work computer. The problem is that when you enter localhost it goes to proxy's address not local address you should bypass it follow this steps

Chrome => Settings => Change proxy settings => LAN Settings => check Bypass proxy server for local addresses.

Solution 9 - C

In Ubuntu, Try sudo ufw allow <port_number> to allow firewall access to both of your server and db.

Solution 10 - C

From the standpoint of a Checkpoint firewall, you will see a message from the firewall if you actually choose Reject as an Action thereby exposing to a propective attacker the presence of a firewall in front of the server. The firewall will silently drop all connections that doesn't match the policy. Connection refused almost always comes from the server

Solution 11 - C

In my case, it happens when the site is blocked in my country and I don't use VPN. For example when I try to access vimeo.com from Indonesia which is blocked.

Solution 12 - C

I had the same message with a totally different cause: the wsock32.dll was not found. The ::socket(PF_INET, SOCK_STREAM, 0); call kept returning an INVALID_SOCKET but the reason was that the winsock dll was not loaded.

In the end I launched Sysinternals' process monitor and noticed that it searched for the dll 'everywhere' but didn't find it.

Silent failures are great!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionZenetView Question on Stackoverflow
Solution 1 - Ca'rView Answer on Stackoverflow
Solution 2 - CRemy LebeauView Answer on Stackoverflow
Solution 3 - CJames BrockView Answer on Stackoverflow
Solution 4 - CRedPandaCuriosView Answer on Stackoverflow
Solution 5 - CAdilView Answer on Stackoverflow
Solution 6 - CJack SunView Answer on Stackoverflow
Solution 7 - CSnapShotView Answer on Stackoverflow
Solution 8 - Cİbrahim ÖzbölükView Answer on Stackoverflow
Solution 9 - Crajeeva9View Answer on Stackoverflow
Solution 10 - CJulio NalundasanView Answer on Stackoverflow
Solution 11 - CAminah NurainiView Answer on Stackoverflow
Solution 12 - CxtoflView Answer on Stackoverflow