尝试连接套接字时ECONNABORTED意味着什么?

Nir*_*iel 7 python sockets

我在ubuntu机器上使用python 2.7.

客户端尝试连接到服务器.我得到了一个EINPROGRESS,预计非阻塞套接字.

要检查连接是否成功,我会执行{connect}的手册页建议:

# EINPROGRESS The socket is nonblocking and the connection cannot be
# completed immediately.  It is possible to select(2) or poll(2) for
# completion by selecting the socket for writing.  After select(2)
# indicates writability, use getsockopt(2) to read the SO_ERROR option at
# level SOL_SOCKET to determine whether connect() completed successfully
# (SO_ERROR is zero) or unsuccessfully (SO_ERROR is one of the usual error
# codes listed here, explaining the reason for the failure)
Run Code Online (Sandbox Code Playgroud)

当服务器脱机时,这给了我一个ECONNREFUSED.到现在为止还挺好.

当连接失败时,我想再试一次.

问题:第二次尝试连接同一个套接字时,{connect}发送给我ECONNABORTED.这个不在{connect}的手册页中.这是什么意思?

App*_*234 14

ECONNABORTED 设置在Linux内核源代码插槽代码的两个位置.

根据errno手册页和/include/asm-generic/errno.h

#define ECONNABORTED 103 /* Software caused connection abort */

所述第一是在定义系统调用的功能accept4/net/socket.c.

相关源代码

1533         if (upeer_sockaddr) {
1534                 if (newsock->ops->getname(newsock, (struct sockaddr *)&address,
1535                                           &len, 2) < 0) {
1536                         err = -ECONNABORTED;
1537                         goto out_fd;
1538                 }
1539                 err = move_addr_to_user((struct sockaddr *)&address,
1540                                         len, upeer_sockaddr, upeer_addrlen);
1541                 if (err < 0)
1542                         goto out_fd;
1543         }
Run Code Online (Sandbox Code Playgroud)

逻辑的相关解释如下.

如果定义了来自用户空间的对等套接字的地址,并且如果新套接字没有名称,则将错误状态设置为ECONNABORTED并转到标签out_fd.

所述第二是在定义该符号的功能inet_stream_connect/net/ipv4/af_inet.c.

相关源代码

645         /* Connection was closed by RST, timeout, ICMP error
646          * or another process disconnected us.
647          */
648         if (sk->sk_state == TCP_CLOSE)
649                 goto sock_error; 

662 sock_error:
663         err = sock_error(sk) ? : -ECONNABORTED;
664         sock->state = SS_UNCONNECTED;
665         if (sk->sk_prot->disconnect(sk, flags))
666                 sock->state = SS_DISCONNECTING;
667         goto out;
Run Code Online (Sandbox Code Playgroud)

逻辑的相关解释如下.

唯一具有转到sock_error标签的代码inet_stream_connect是检查套接字是否被RST,超时,另一个进程或错误关闭.

sock_error标签中如果我们可以恢复套接字错误报告,请执行此操作,否则将错误状态恢复到ECONNABORTED

Celada的评论一样,我也建议每次打开一个新的套接字.