1

我需要在端口 55005 向服务器发送多个请求(命令)。我的第一个请求获取过程成功并且我也收到了输出。但是对于第二个请求,它会给出错误(WSAESHUTDOWN - 错误 10058)。在第一次请求后,我调用了 shutdown(ConnectSocket, SD_SEND); 然后只有服务器处理第一个请求并向我发送输出。现在可以重新打开套接字处理下一个请求吗?**

关机后如何处理多个请求(ConnectSocket,SD_SEND)?提前感谢您的建议。

我发送第一个请求,等待第一个回复。在第一次请求回复后,我可以发送下一个请求。这是业务逻辑要求。我不想为每个请求打开一个新连接。

快照代码从这里开始---------------->

**//This for loop will send multiple request to Servers.**
for(it = CommandList.begin(); it != CommandList.end() ; it++ )
{
    //get each command request & send it to server.
    std::string  sendBuf; // =  (*it);
     sendBuf= *it;
    int length = (int)strlen(sendBuf.c_str());
    //----------------------
    // Send an initial buffer
    iResult = send( ConnectSocket, (char*)sendBuf.c_str(), length, 0 );
    if (iResult == SOCKET_ERROR) {
        wprintf(L"send failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

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


    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        wprintf(L"shutdown failed with error: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            wprintf(L"Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            wprintf(L"Connection closed\n");
        else
            wprintf(L"recv failed with error: %d\n", WSAGetLastError());

    } while( iResult > 0 );
}
4

1 回答 1

0

shutdown正如 Joachim 所说,在连接完成之前不要打电话。这个 TCP 套接字是一个“流”......东西可以随意来回。作为程序员,您必须确定“请求”和“响应”的开始和结束位置......不要使用shutdown它。

于 2013-02-06T13:15:54.583 回答