TCP服务端修改

This commit is contained in:
李广豪 2025-06-18 18:17:05 +08:00
parent 6e24151113
commit c0a5ddfeb7

View File

@ -122,7 +122,7 @@ void CFastMainDialog::ProcessPipeMsg(int lMsgId, char* pData, int lLen)
} }
// TCP服务端线程 // TCP服务端线程
UINT CFastMainDialog::TCPListenerThread(LPVOID pParam) { /*UINT CFastMainDialog::TCPListenerThread(LPVOID pParam) {
CFastMainDialog* pThis = (CFastMainDialog*)pParam; CFastMainDialog* pThis = (CFastMainDialog*)pParam;
SOCKET hServer = pThis->m_hServerSocket; SOCKET hServer = pThis->m_hServerSocket;
@ -139,6 +139,34 @@ UINT CFastMainDialog::TCPListenerThread(LPVOID pParam) {
closesocket(hClient); closesocket(hClient);
} }
return 0; return 0;
}*/
UINT CFastMainDialog::TCPListenerThread(LPVOID pParam) {
CFastMainDialog* pThis = (CFastMainDialog*)pParam;
SOCKET hServer = pThis->m_hServerSocket;
while (true) {
SOCKET hClient = accept(hServer, NULL, NULL);
if (hClient == INVALID_SOCKET) continue;
// 禁用Nagle算法可选
int optval = 1;
setsockopt(hClient, IPPROTO_TCP, TCP_NODELAY, (char*)&optval, sizeof(optval));
// 持续接收数据直到连接关闭
char buffer[4096];
while (true) {
int bytesRecv = recv(hClient, buffer, sizeof(buffer), 0);
if (bytesRecv <= 0) { // 连接断开或错误
LogOutToFile("[TCP] Client disconnected or error: %d", WSAGetLastError());
break;
}
CString strMsg(buffer, bytesRecv);
pThis->ProcessTCPMessage(strMsg);
}
closesocket(hClient); // 最终关闭连接
}
return 0;
} }
// 处理收到的TCP消息 // 处理收到的TCP消息