在重新启动的服务器上使用 connect() 重新连接返回 - 传输端点已连接 [英] Reconnecting with connect() on restarted server returns -Transport endpoint is already connected

查看:41
本文介绍了在重新启动的服务器上使用 connect() 重新连接返回 - 传输端点已连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经阅读了这篇文章以寻找正确的答案,但它似乎不符合我的目的.

Okay, I've read this post in search for the right answer, but it does not seem to serve my purpose.

这个问题

现在,解决问题:

我在 C 中有一个传统的客户端 - 服务器架构(所有套接字都是非阻塞的),其中服务器正在侦听传入连接,客户端尝试连接.第一次连接成功,一切正常,直到我在服务器上按 Ctrl + C.

I have a conventional client-server architecture in C (all sockets are non-blocking), where the server is listening for incoming connections and the client tries to connect. The first connect succeeds and everything goes on just fine until I press Ctrl + C on my server.

代码的客户端检测到连接丢失并设置重试计时器.客户端代码应该通过在每个计时器弹出时使用 POSIX 间隔计时器一次又一次地尝试在服务器上重新连接.但是,它不会关闭套接字或重新开始.现在,每次重试连接时,connect() 都会返回

The client side of the code detects that the connection is lost and arms a retry timer. The client code is supposed to try a reconnect on the server again and again by using the POSIX interval timers on each timer popping. It however, does not close the socket or start out afresh. Now, every time it retries the connection, the connect() returns

传输端点已连接

即使在使用 SO_REUSEADDR 并成功启动的服务器重新启动后,连接也没有完成.我需要实现的一件事是服务器上的信号处理程序,用于按 Ctrl+C 关闭.

Even after restarting the server, which uses the SO_REUSEADDR and successfully starts, the connect does not complete. One thing that I will need to implement is the signal handler on the server for the shutdown on Ctrl+C.

但是,每次断开连接时,我是否需要关闭客户端的套接字描述符并重新开始,或者有没有办法摆脱这种情况?

But still, do I need to close the socket descriptor on the client side and start afresh every time a disconnect happens, or is there a way out of this?

推荐答案

套接字不能重复使用.

一旦服务的套接字在两个方向上的连接都断开,则该套接字将无法使用.

Once the connection a socket served has gone down in both directions, the socket is unusable.

close() 连接丢失时的客户端套接字并为新连接创建一个新套接字.

close() the client socket on loss of connection and create a new socket for a new connection.

更新(基于以下评论):

在 OP 的情况下,一侧(服务器端)出现故障(通过服务器进程结束).这意味着该进程持有的所有套接字都被隐式地close()ed,因此在两个方向上都被shutdown().

In the OP's case one side (the server side) went down (by means of the server process ending). This implies all sockets held by this process are implicitly close()ed and therefore shutdown() in both directions.

这篇关于在重新启动的服务器上使用 connect() 重新连接返回 - 传输端点已连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆