强制 recv (Windows C++) 函数阻塞,直到读取 N 个字节

Force recv (Windows C++) function to block until N bytes are read

我正在使用 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);