gpt4 book ai didi

c - 设置套接字接收缓冲区大小(不带 block )

转载 作者:行者123 更新时间:2023-11-30 16:46:22 24 4
gpt4 key购买 nike

我在使用 C 语言的 Socket 时遇到问题。数据包大小设置为 3072在 socks 选项中这样:

int         recvBuff = 3072;
int recvLowAt = 2;

if (setsockopt(sock, SOL_SOCKET, SO_RCVLOWAT, &recvLowAt, sizeof(recvLowAt)) < 0) {
LOGE("Error setsockopt rcvlowat -> %s\n", strerror(errno));
close(sock);
return -1;
}

if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &recvBuff, sizeof(recvBuff)) < 0) {
LOGE("Error setsockopt rcvbuf -> %s\n", strerror(errno));
close(sock);
return -1;
}

有时数据包是 < 3072即使它小于 3072 我也想得到它

if ((size = recv(sock, buffer, BUFF_SIZE, 0)) < 0) {
LOGE("Error recv -> %s\n", strerror(errno));
size = 0;
} else {
LOGI("%d bytes received\n", size);
}

输出是:

1448 bytes received
2896 bytes received
1448 bytes received

我想要这样的输出:

Output is :

3072 bytes received
3072 bytes received
3072 bytes received
500 bytes received
Whole data has been sended !

我尝试使用MSG_WAITALL作为 recv 中的标志但最后一个数据包没有发送...

我可以帮忙吗?谢谢!

最佳答案

套接字接收调用并不总是返回发送者在其发送调用中发送的全部数据。您需要继续调用recv并检查可能是从长度或数据内容是否获得完整数据

关于c - 设置套接字接收缓冲区大小(不带 block ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43781568/

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