gpt4 book ai didi

c++ - 服务器断开连接时发送到套接字

转载 作者:太空狗 更新时间:2023-10-29 12:09:32 24 4
gpt4 key购买 nike

我按顺序使用下面的函数

int sendData(int const Socket, unsigned char const * const Data, size_t const Length)
{
return send(Socket, Data, Length, 0);
}

函数返回发送字节数。

如果我断开服务器,此函数返回发送字节数等于长度。看起来系统不知道另一端已断开连接。

如何解决这个问题?

最佳答案

通过套接字发送数据是一种低级操作。成功发送并不能保证另一端会成功接收。

The Manual说:

No indication of failure to deliver is implicit in a send()

所以服务器必须发回一个确认给客户端确认。

附带说明一下,您的函数只是重新创建系统 write 函数。根据The Manual :

The only difference between send() and write(2) is the presence of flags. With a zero flags argument, send() is equivalent to write(2).

关于c++ - 服务器断开连接时发送到套接字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50604532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com