重新连接到服务器后如何修复 EPIPE

How to fix EPIPE after reconnecting to Server

我有一个 C socket 客户端程序,其中一个 thread 用于接收数据,另一个用于发送。如果 server 关闭,则发送者得到 EPIPE。如果我重新连接相同的 socket 然后它可以接收数据但发件人仍然收到 EPIPE.

如何解决这个问题?

更新: 实际上发件人似乎在发送数据,因为我看到发送的字节数。但是 errno 仍然设置为 broken pipe。之前我只检查errno。不应该改成successful吗?

If I reconnect the same socket then it can receive data but the sender still gets EPIPE.

这只能意味着发送方仍在通过旧套接字发送;还有你没有关闭旧套接字。

sender seems to send data as I see number of byte sent. But errno is still set to broken pipe. Before I only checked errno. Shouldn't it be changed to successful?

没有。仅当前一个系统调用返回 -1 时检查 errno 才有效。示例:

int rc = send(...);
if (rc < 0)
{
    if (errno == EWOULDBLOCK) // or EAGAIN *and* we are in non-blocking mode
    {
        // queue the write and return to the select() loop
    }
    else
    {
        perror("send"); // for example
    }
}
else
{
    // write succeeded ...
}