[英]recv reads incomplete packet
I have simple function that responsible on receiving packets via socket. 我有简单的功能,负责通过套接字接收数据包。
if((recv_size = recv(sock , rx , 50000 ,0)) == SOCKET_ERROR)
{
...
} else
{
...
}
I found that sometimes I receiv incompleate packet. 我发现有时我收到的包裹不完整。 Why? 为什么? Mybe I should use recv
for several times? 我应该多次使用recv
吗? Packet length never exceeds 50000 bytes. 数据包长度不得超过50000字节。
I use TCP socket. 我使用TCP套接字。
If you're using TCP it's expected. 如果您使用的是TCP,那就可以了。 TCP is a streaming protocol, it doesn't have "packets" or message boundaries, and you can have received all of the "message" or part of it, or even multiple messages. TCP是一种流协议,它没有“数据包”或消息边界,并且您可以接收到所有“消息”或消息的一部分,甚至是多条消息。 So you might have to call recv
multiple times to receive a complete message. 因此,您可能必须多次调用recv
才能收到完整的消息。
However, since TCP doesn't have message boundaries, you have to implement them yourself on top of TCP, for example by sending the length of the message in a fixed-size header, or have some special end-of-message marker. 但是,由于TCP没有消息边界,因此您必须在TCP之上自己实现它们,例如,通过在固定大小的标头中发送消息的长度,或者使用一些特殊的消息结束标记。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.