ChatGPT解决这个技术问题 Extra ChatGPT

What can be the reasons of connection refused errors?

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?

I just got this error, because of a mistake of the server where I host my website. The server could still be pinged, downforeveryoneorjustme.com showed that it's not just me and I could also still access it via FTP. After a couple of minutes the mistakes was resolved.
@moose: ping does not tell you whether a specific port is accessible ad listening, only whether the IP address is reachable. But connect() depends on a specific IP and port being ready for use.
Server Fault has a canonical question about Connection Refused.
Here's a succinct explanation of Connection Refused.
I got a connection refused on Ubuntu because I was trying to receive connection on 'localhost' but 'localhost' was not properly configured on my machine. Changing 'localhost' to '' (Python) solved the problem.

R
Remy Lebeau

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

The port is not open on the destination machine. The port is open on the destination machine, but its backlog of pending connections is full. 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.


Firewalls would normally give timeout errors, as the connect (SYN) packet is just discarded. Connection refused is because the server has received and rejected the SYN packet.
Not always, as firewalls can be configured to reject rather than drop the packets.
connect() with incorrect Server IP address and port number configuration in client program will also result in errno 111.
In addition: when you need to acces https, but you have specified http://... this error can also occur.
@a'r How would you know the status of backlog?
J
Josh Darnell

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.


There's also one edge case as explained here: softlab.ntua.gr/facilities/documentation/unix/unix-socket-faq/… . basically if you are stress testing your client/server app, and you are using the naive approach as mentioned in 3.6, "connection refused" error may happen, and it's different case from the above two.
This needs to be upvoted. Once the obvious cause (nothing listening) has been resolved, then this is actually the most likely explanation, and fixing it can be a complex matter.
if the backlog of queued inbound requests has reached it's maximum, how can we fix this?
is a backlog queued by IP? I tried using telnet email-smtp.eu-west-1.amazonaws.com 25 from an EC2 instance and my local machine, the EC2 instance telnet call is giving connection refused while from my local machine it works
"nothing is listening" is probably caused by the machine being up but the server (software) e.g. Apache is not running.
C
Community

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

You sent a TCP SYN packet to the other host. 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.


Great answer. This should be the one accepted!
R
RedPandaCurios

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...


This is just one of several possible conditions that can cause the error.
The port could be open, but this error could still occur.
Server not actually running is a very common scenario in development environments, where teams are paying closer attention to production servers.
A
Adil

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.


J
Jack Sun

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

netstat -a helped
C
Community

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.


İ
İbrahim Özbölük

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.


This fixed me up. But on my version of Chrome it was called "Open your computer's proxy settings" which opened up my MacOS proxy settings. From there I added a wildcard for the TLD I was using (*.test) for my local addresses under "Bypass Proxy Settings for Hosts & Domains"
r
rajeeva9

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


J
Julio Nalundasan

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


A
Aminah Nuraini

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.


x
xtofl

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!


INVALID_SOCKET has nothing to do with 'connection refused'. 'Silent failures' can only occur if your code is missing required error handling.
you mean I should have had a check that the dll was loaded? you are probably right.