Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 当客户端只关闭发送连接的一半时,为什么HTTP服务器要关闭连接?_C++_C_Windows_Http_Network Programming - Fatal编程技术网

C++ 当客户端只关闭发送连接的一半时,为什么HTTP服务器要关闭连接?

C++ 当客户端只关闭发送连接的一半时,为什么HTTP服务器要关闭连接?,c++,c,windows,http,network-programming,C++,C,Windows,Http,Network Programming,可能重复: 我正在尝试通过WinSock2发送HTTP请求。我终于做到了,但是,我不明白为什么当我关闭发送连接的一半时,连接会被关闭 根据: 当客户端或服务器希望超时时,它应该在传输连接上发出一个优雅的关闭。客户机和服务器都应该不断监视传输的另一端,并根据需要做出响应 服务器不应在发送响应的中间关闭连接,除非怀疑有网络或客户端故障。 我想套接字仍然会接收消息,直到服务器没有任何东西要发送,但服务器只是意外地关闭了连接 C:\Users\970067a\Desktop>gcc test.c

可能重复:

我正在尝试通过WinSock2发送HTTP请求。我终于做到了,但是,我不明白为什么当我关闭发送连接的一半时,连接会被关闭

根据:

当客户端或服务器希望超时时,它应该在传输连接上发出一个优雅的关闭。客户机和服务器都应该不断监视传输的另一端,并根据需要做出响应

服务器不应在发送响应的中间关闭连接,除非怀疑有网络或客户端故障。 我想套接字仍然会接收消息,直到服务器没有任何东西要发送,但服务器只是意外地关闭了连接

C:\Users\970067a\Desktop>gcc test.c -lWs2_32 && a
Bytes Sent: 59
Bytes received: 787
Bytes received: 222
Connection closed

C:\Users\970067a\Desktop>gcc test.c -lWs2_32 -D SHUTDOWN_SD_SEND && a
Bytes Sent: 59
Connection closed

以下是我的部分源代码:

...

iResult = getaddrinfo("www.google.com", "http", &hints, &result);
if (iResult != 0) {
    printf("getaddrinfo failed: %d\n", iResult);
    WSACleanup();
    return 1;
}

...

int recvbuflen = DEFAULT_BUFLEN;

char *sendbuf = "GET / HTTP/1.1\r\nhost: www.google.com\r\nConnection: close\r\n\r\n";
char recvbuf[DEFAULT_BUFLEN];

// Send an initial buffer
iResult = send(ConnectSocket, sendbuf, (int)strlen(sendbuf), 0);
if (iResult == SOCKET_ERROR) {
    printf("send failed: %d\n", WSAGetLastError());
    closesocket(ConnectSocket);
    WSACleanup();
    return 1;
}

printf("Bytes Sent: %ld\n", iResult);

// shutdown the connection for sending since no more data will be sent
// the client can still use the ConnectSocket for receiving data
#ifdef SHUTDOWN_SD_SEND
iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
  printf("shutdown failed: %d\n", WSAGetLastError());
  closesocket(ConnectSocket);
  WSACleanup();
  return 1;
}
#endif

// Receive data until the server closes the connection
do {
    iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    if (iResult > 0)
        printf("Bytes received: %d\n", iResult);
    else if (iResult == 0)
        printf("Connection closed\n");
    else
        printf("recv failed: %d\n", WSAGetLastError());
} while (iResult > 0);

// cleanup
closesocket(ConnectSocket);
WSACleanup();

...

RFC2616中没有关于一半关闭或关闭的信息。您不应该使用HTTP进行这些操作。

但为什么要使用这种设计?这对持久性连接不好?这可能很有趣。