I need to receive "multipart/form-data" upload request which contains many binary files. From the tutorials I read, this is the way I came up with to instruct the server how to know when to stop receiving HTTP Request packets:
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
}
}
The current solution isn't reliable, for example it doesn't have a timeout value. Are there any better ways to receive HTTP Requests?