我正在学习Windows中的套接字,也是HTTP协议。 所以我正在做一些保持活着的测试,但由于某种原因,recv挂起约5秒钟,这里是:
VOID TestWinsock()
{
WSADATA WsaData;
addrinfo hints;
addrinfo* hResult;
SOCKET hsocket;
int Result;
WSAStartup(MAKEWORD(2, 2), &WsaData);
RtlZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
Result = getaddrinfo("localhost", "80", &hints, &hResult);
if (Result != 0)
{
return;
}
hsocket = socket(hResult->ai_family, hResult->ai_socktype, hResult->ai_protocol);
if (hsocket == INVALID_SOCKET)
{
return;
}
Result = connect(hsocket, hResult->ai_addr, (int)hResult->ai_addrlen);
if (Result == SOCKET_ERROR)
{
return;
}
char* POSTContent;
char HTTPRequestBuffer[1024];
char RecvBuffer[1024];
char* HTTPRequest =
"POST %s HTTP/1.0\r\n"
"Host: %s\r\n"
"connection: keep-alive\r\n"
"Content-type: application/x-www-form-urlencoded\r\n"
"Content-Length: %u\r\n"
"\r\n"
"%s"
"\r\n"
"\r\n";
POSTContent = "variable1=10";
wsprintfA(
HTTPRequestBuffer,
HTTPRequest,
"/tests/test.php",
"127.0.0.1",
strlen(POSTContent),
POSTContent
);
int sent = send(hsocket, HTTPRequestBuffer, strlen(HTTPRequestBuffer), 0);
RtlZeroMemory(RecvBuffer, sizeof(RecvBuffer));
// here recv blocks for about 5 seconds, yeah, it reads only 100 bytes, tried with something like sizeof(RecvBuffer) as well.
recv(hsocket, RecvBuffer, 100, 0);
....
}
我想知道如何解决这个问题,或者哪个是使用keep-alive的正确方法。
答案 0 :(得分:2)
在您的HTTP请求中,您发送的额外\r\n\r\n
未包含在Content-Length
中,因此数据和宣布的长度不匹配。 HTTP标准说:
在消息正文的消息中给出Content-Length时 允许,其字段值必须与OCTET的数量完全匹配 消息体。
MUST
很重要,这意味着服务器完全有权拒绝您的请求。您指定keep-alive
的事实也可能意味着服务器等待下一个有效请求。由于您只发送4个字节,服务器最终会在5s后关闭连接,并且可能发送错误(这部分只是关于实际发生的事情的推测)。