From c0a5ddfeb782aeec7cd867ca642ca426d4903fed Mon Sep 17 00:00:00 2001 From: LGH <1746689524@qq.com> Date: Wed, 18 Jun 2025 18:17:05 +0800 Subject: [PATCH] =?UTF-8?q?TCP=E6=9C=8D=E5=8A=A1=E7=AB=AF=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plugin/Fast/FastMainDialog.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/Plugin/Fast/FastMainDialog.cpp b/Plugin/Fast/FastMainDialog.cpp index 7de1477..645a1d1 100644 --- a/Plugin/Fast/FastMainDialog.cpp +++ b/Plugin/Fast/FastMainDialog.cpp @@ -122,7 +122,7 @@ void CFastMainDialog::ProcessPipeMsg(int lMsgId, char* pData, int lLen) } // TCP服务端线程 -UINT CFastMainDialog::TCPListenerThread(LPVOID pParam) { +/*UINT CFastMainDialog::TCPListenerThread(LPVOID pParam) { CFastMainDialog* pThis = (CFastMainDialog*)pParam; SOCKET hServer = pThis->m_hServerSocket; @@ -139,6 +139,34 @@ UINT CFastMainDialog::TCPListenerThread(LPVOID pParam) { closesocket(hClient); } 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消息