简体   繁体   English

在重新启动的服务器上使用 connect() 重新连接返回 - 传输端点已连接

[英]Reconnecting with connect() on restarted server returns -Transport endpoint is already connected

Okay, I've read this post in search for the right answer, but it does not seem to serve my purpose.好的,我已经阅读了这篇文章以寻找正确的答案,但这似乎不符合我的目的。

This Question 这个问题

Now, getting to the trouble:现在,遇到麻烦:

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.我在 C 中有一个传统的客户端 - 服务器架构(所有套接字都是非阻塞的),其中服务器正在侦听传入连接而客户端尝试连接。 The first connect succeeds and everything goes on just fine until I press Ctrl + C on my server.第一次连接成功,一切正常,直到我在我的服务器上按 Ctrl + C。

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.客户端代码应该通过在每个计时器弹出时使用 POSIX 间隔计时器一次又一次地尝试在服务器上重新连接。 It however, does not close the socket or start out afresh.但是,它不会关闭套接字或重新开始。 Now, every time it retries the connection, the connect() returns现在,每次重试连接时,connect() 都会返回

Transport endpoint is already connected传输端点已连接

Even after restarting the server, which uses the SO_REUSEADDR and successfully starts, the connect does not complete.即使在使用 SO_REUSEADDR 并成功启动的服务器重新启动后,连接也不会完成。 One thing that I will need to implement is the signal handler on the server for the shutdown on Ctrl+C.我需要实现的一件事是服务器上的信号处理程序,用于按 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?但是,我是否需要在客户端关闭套接字描述符并在每次断开连接时重新开始,或者有没有办法解决这个问题?

sockets cannot be reused.套接字不能重复使用。

Once the connection a socket served has gone down in both directions, the socket is unusable.一旦服务的套接字在两个方向上的连接都出现故障,则该套接字将无法使用。

close() the client socket on loss of connection and create a new socket for a new connection. close()在失去连接时close()客户端套接字并为新连接创建一个新套接字。


Update (based on the comments below):更新(基于以下评论):

In the OP's case one side (the server side) went down (by means of the server process ending).在 OP 的情况下,一侧(服务器端)出现故障(通过服务器进程结束)。 This implies all sockets held by this process are implicitly close() ed and therefore shutdown() in both directions.这意味着该进程持有的所有套接字都被隐式地close() ed,因此在两个方向上都被shutdown()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM