gpt4 book ai didi

c - send() 总是发送整个缓冲区吗?

转载 作者:IT王子 更新时间:2023-10-29 00:04:02 26 4
gpt4 key购买 nike

send() 应返回发送的字节数或错误代码,但我发现的所有示例仅检查错误代码,而不检查发送的字节数。

//typical example
int cnt=send(s,query,strlen(query),0);
if (cnt < 0) return(NULL);
//Hey, what about cnt < strlen(query)?

最佳答案

问:“send()”总是返回整个缓冲区吗?

答:不一定。

来自 Beej 的指南: * http://beej.us/guide/bgnet/html/multi/syscalls.html#sendrecv

send() returns the number of bytes actually sent out—this might be less than the number you told it to send! See, sometimes you tell it to send a whole gob of data and it just can't handle it. It'll fire off as much of the data as it can, and trust you to send the rest later. Remember, if the value returned by send() doesn't match the value in len, it's up to you to send the rest of the string. The good news is this: if the packet is small (less than 1K or so) it will probably manage to send the whole thing all in one go. Again, -1 is returned on error, and errno is set to the error number.

问:“recv()”是否总是读取整个缓冲区?

答:不,绝对不是。您永远不要假设您收到的缓冲区是“整个消息”。或者假设您收到的消息来自一个单个消息。

这是一个很好的简短解释。它适用于 Microsoft/C#,但适用于所有 套接字 I/O,使用任何语言:

关于c - send() 总是发送整个缓冲区吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12657063/

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