C语言 连接拒绝错误的原因可能是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2333400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:36:40  来源:igfitidea点击:

What can be the reasons of connection refused errors?

csocketsconnection-refused

提问by Zenet

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.

我正在尝试使用另一个客户端用 C 编写服务器程序,例如,当我尝试通过端口 2080 进行连接时出现此错误。

connection refused

What can be the reasons of this error?

此错误的原因可能是什么?

采纳答案by a'r

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

  1. 目标计算机上的端口未打开。

  2. 该端口在目标机器上是打开的,但其未决连接的积压已满。

  3. 客户端和服务器之间的防火墙阻止访问(还要检查本地防火墙)。

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.

检查防火墙和端口是否打开后,使用 telnet 连接到 ip/端口以测试连接。这会消除您的应用程序中的任何潜在问题。

回答by Remy Lebeau

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.

还有一个尚未提及的原因 - 侦听端口实际上是打开的并且正在被积极使用,但是其排队的入站连接请求的积压已达到最大值,因此没有可用空间来排队入站连接请求片刻。服务器代码还没有调用 accept() 足够多的时间来完成为新队列项目清除可用插槽。

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.

稍等片刻,然后再次尝试连接。不幸的是,无法区分“端口根本没有打开”和“端口打开但现在太忙”。它们都使用相同的通用错误代码。

回答by James Brock

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

如果您尝试打开到另一台主机的 TCP 连接并看到错误“连接被拒绝”,则意味着

  1. You sent a TCP SYN packet to the other host.
  2. Then you received a TCP RST packet in reply.
  1. 您向另一台主机发送了一个 TCP SYN 数据包。
  2. 然后您收到了一个 TCP RST 数据包作为回复。

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.

RST 是 TCP 数据包上的一个位,表示应该重置连接。通常这意味着另一台主机已收到您的连接尝试并主动拒绝您的 TCP 连接,但有时中间防火墙可能会阻止您的 TCP SYN 数据包并向您发送 TCP RST。

See https://tools.ietf.org/html/rfc793page 69:

请参阅https://tools.ietf.org/html/rfc793第 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.

同步接收状态

如果设置了 RST 位

如果这个连接是用被动的 OPEN 发起的(即来自 LISTEN 状态),则将此连接返回到 LISTEN 状态并返回。不需要通知用户。如果这个连接是用一个活动的 OPEN 发起的(即来自 SYN-SENT 状态),那么连接被拒绝,向用户发出“连接被拒绝”的信号。在任何一种情况下,都应该删除重传队列中的所有段。而在活动OPEN情况下,进入CLOSED状态,删除TCB,返回。

回答by 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.

因此,要么您连接到了错误的 IP 地址,要么连接到了错误的端口,要么服务器正在侦听错误的端口,或者实际上并未运行。

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

一个常见的错误是在以网络字节顺序绑定或连接时没有指定端口号...

回答by 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:

在服务器端检查它是否正在侦听端口 2080。首先尝试通过向该端口发出 telnet 来在服务器机器上确认它:

telnet localhost 2080

远程登录本地主机 2080

If it is listening, it is able to respond.

如果它在听,它就能够做出反应。

回答by Hyman Sun

1.Check your server status.

1.检查您的服务器状态。

2.Check the port status.

2.检查端口状态。

For example 3306 netstat -nupl|grep 3306.

例如 3306 netstat -nupl|grep 3306

3.Check your firewalls. For example add 3306

3.检查你的防火墙。例如添加 3306

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

回答by SnapShot

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:

尽管您的情况似乎并非如此,但有时连接被拒绝错误也可能表明您的网络存在 IP 地址冲突。您可以通过运行来搜索可能的 IP 冲突:

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

and

arping <ipaddress>

ThisAskUbuntu question has some more information also.

这个AskUbuntu 问题也有更多信息。

回答by ?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

我的工作电脑也有同样的问题。问题是,当您输入 localhost 时,它会转到代理的地址而不是本地地址,您应该按照以下步骤绕过它

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

Chrome => 设置 => 更改代理设置 => LAN 设置 => 检查本地地址的绕过代理服务器。

回答by 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

从 Checkpoint 防火墙的角度来看,如果您实际上选择拒绝作为操作,您将看到来自防火墙的消息,从而向预期攻击者暴露服务器前面防火墙的存在。防火墙将默默地丢弃所有与策略不匹配的连接。连接被拒绝几乎总是来自服务器

回答by rajeeva9

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

在 Ubuntu 中,尝试 sudo ufw allow <port_number>允许防火墙访问您的服务器和数据库。