gpt4 book ai didi

c - 资源暂时不可用 - MSG_DONTWAIT 标志

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:43:35 25 4
gpt4 key购买 nike

我在 c 中使用 tcp 套接字服务器和客户端。使用 AF_INET、SOCK_STREAM 和 IPPROTO_TCP

我在客户端的发送功能

sent_now = send(sockId, buffer + sent_total, len - sent_total, MSG_DONTWAIT);

当我发送大小为 20K-40KB 的 MSG 时,它工作正常。当我发送 MSG 到 365K(这就是我需要的大小)时,我在服务器端收到以下错误:资源暂时不可用!

我在服务端的接收函数

totalRecvMsgSize = recieve (clntSocket, Buffer, RCVBUFSIZE, MSG_DONTWAIT);

我发送的是int数据类型。

如何在一次发送和接收操作中接收所有数组?

旧帖

有一个帖子What can cause a “Resource temporarily unavailable” on sock send() command , Davide Berra 说

That's because you're using a non-blocking socket and the output buffer is full.

From the send() man page

When the message does not fit into the send buffer of the socket, send() normally blocks, unless the socket has been placed in non-block- ing I/O mode. In non-blocking mode it would return EAGAIN in this case.

EAGAIN is the error code tied to "Resource temporarily unavailable"

Consider using select() to get a better control of this behaviours

我的问题是:

*1) 如何在一次发送和接收操作中接收所有数组? *

2) select() 对这个数组大小有帮助吗?

3) 我可以更改缓冲区大小吗?如何更改?

最佳答案

因为这是一个流套接字,所以您在 send() 上使用循环, 以及 recv() 上的 MSG_WAITALL .

考虑这些辅助函数:

/* Receive the entire buffer.
* Returns 0 if success, nonzero errno otherwise.
*/
static int recv_all(const int sockfd, void *buffer, const size_t len)
{
ssize_t n;

n = recv(sockfd, buffer, len, MSG_WAITALL);
if (n == 0)
return errno = EPIPE; /* Other end will not send more data */
else
if (n == -1)
return errno;
else
if (n < -1)
return errno = EIO; /* Should never occur */
else
if (n != (ssize_t)len)
return errno = EINTR; /* Interrupted, or sender goofed */
else
return 0;
}

/* Send the entire buffer.
* Returns 0 if success, nonzero errno otherwise.
*/
static int send_all(const int sockfd, const void *buffer, const size_t len)
{
const char *pos = (const char *)buffer;
const char *const end = (const char *)buffer + len;
ssize_t n;

while (pos < end) {

n = send(sockfd, pos, (size_t)(end - pos), 0);
if (n > 0)
pos += n;
else
if (n != -1)
return errno = EIO;
else
return errno;
}
}

关于c - 资源暂时不可用 - MSG_DONTWAIT 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39959115/

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