我需要接收包含许多二进制文件的“multipart / form-data”上传请求。从我读过的教程中,这是我提出的指示服务器如何知道何时停止接收HTTP请求数据包的方式:
while (true)
{
#ifdef _WIN32
u_long arg;
if (ioctlsocket(socket, FIONREAD, &arg) == SOCKET_ERROR)
throw std::exception("ioctlsocket");
#else
int arg;
if (ioctl(socket, FIONREAD, &arg) == -1) throw std::exception("ioctl");
#endif
else if (arg == 0) break;
char buf[1024];
auto received = recv(socket, buf, 1024, 0);
#ifdef _WIN32
if (received == SOCKET_ERROR) throw std::exception("recv");
#else
if (received == -1) throw std::exception("recv");
#endif
else if (received == 0) break;
else
{
// work with the buffer
}
}
当前解决方案不可靠,例如它没有超时值。有没有更好的方法来接收HTTP请求?