gpt4 book ai didi

c++ - WinSock 发送多个请求和关机问题

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:26 25 4
gpt4 key购买 nike

我需要在端口 55005 向服务器发送多个请求(命令)。我的第一个请求得到处理成功,我也收到了。但是对于第二个请求,它会给出错误(WSAESHUTDOWN - 错误 10058)。在第一次请求后我打电话 关闭(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 );
}

最佳答案

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

关于c++ - WinSock 发送多个请求和关机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14725674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com