What can be the reasons of connection refused erro

2019-01-04 08:44发布

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?

11条回答
Summer. ? 凉城
2楼-- · 2019-01-04 09:24

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://tools.ietf.org/html/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.
查看更多
乱世女痞
3楼-- · 2019-01-04 09:24

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

查看更多
姐就是有狂的资本
4楼-- · 2019-01-04 09:29

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.

查看更多
Explosion°爆炸
5楼-- · 2019-01-04 09:30

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.

查看更多
三岁会撩人
6楼-- · 2019-01-04 09:31

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.

查看更多
forever°为你锁心
7楼-- · 2019-01-04 09:36

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

查看更多
登录 后发表回答