我正在使用 this客户端-服务器架构的代码。代码可以轻松运行,没有任何问题。然而,TCP 将大数据包分成多个小数据包。问题来自 recv 函数。我是 Windows 编程的新手,想知道是否有类似 read 函数的东西会阻塞,直到它从客户端套接字读取所有 N 个字节(就像在 Linux 中,当你想从客户端套接字 fd 读取时一样)。有什么想法吗?
最佳答案
Windows recv
documentation列出一个 MSG_WAITALL
标志,它将使调用等待直到给定缓冲区完全填满:
MSG_WAITALL The receive request will complete only when one of the following events occurs:
- The buffer supplied by the caller is completely full.
- The connection has been closed.
- The request has been canceled or an error occurred.
例子:
iResult = recv(ConnectSocket, recvbuf, recvbuflen, MSG_WAITALL);
关于c++ - 强制 recv (Windows C++) 函数阻塞,直到读取 N 个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33978489/