如何使用 winsock2 实现与 c++ 的多个并行连接?

How to implement multiple parallel connections with c++ using winsock2?

本文关键字:并行连接 c++ 何使用 winsock2 实现      更新时间:2023-10-16

我正在尝试实现一个简单的服务器,在VSC中使用c ++和winsock2,它可以同时接受多个客户端并与之通信。我尝试使用多线程和一组客户端,似乎我可以连接到多个客户端,但我一次只能与一个客户端通信。一旦我关闭连接,下一个客户端就会连接。我相信这与线程的连接功能有关。但是我找不到以不同的方式解决它的方法。

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <thread>
#pragma comment(lib, "Ws2_32.lib")
#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"
#define MAXIMUM_CONNECTIONS 10
int connectionsCount = 0;
SOCKET clients[MAXIMUM_CONNECTIONS];
using namespace std;
void communicatingWithClient(SOCKET*);
void listenForConnection(SOCKET*);
int main()
{
WSADATA wsaData;
int iResult;
SOCKET ListenSocket = INVALID_SOCKET;
struct addrinfo *result = NULL;
struct addrinfo hints;
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
if (iResult != 0)
{
printf("WSAStartup failed with error: %dn", iResult);
WSACleanup();
return 1;
}
printf("WSA startedn");
ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
// Resolve the local address and port to be used by the server
iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
if (iResult != 0)
{
printf("getaddrinfo failed: %dn", iResult);
freeaddrinfo(result);
WSACleanup();
return 1;
}
// Create a SOCKET for the server to listen for client connections
ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
if (ListenSocket == INVALID_SOCKET)
{
printf("Error at socket(): %dn", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
printf("ListenSocket createdn");
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
printf("bind failed with error: %dn", WSAGetLastError());
freeaddrinfo(result);
closesocket(ListenSocket);
WSACleanup();
return 1;
}
freeaddrinfo(result);
thread listenThread(listenForConnection, &ListenSocket);
listenThread.join();
closesocket(ListenSocket);
WSACleanup();
return 0;
}
void listenForConnection(SOCKET *ListenSocket)
{
int iResult;
// Listen on the ServerSocket forever
while(true)
{
printf("While Loop for listening started...n");
iResult = listen( *ListenSocket, SOMAXCONN);
if (iResult == SOCKET_ERROR )
{
printf( "Listen failed with error: %dn", WSAGetLastError() );
closesocket(*ListenSocket);
WSACleanup();
return;
}
printf("Accepting client socket..n");
// Accept a client socket
if (connectionsCount < 10) 
{
clients[connectionsCount] = accept(*ListenSocket, NULL, NULL);
if (clients[connectionsCount] == INVALID_SOCKET)
{
printf("accept failed: %dn", WSAGetLastError());
closesocket(*ListenSocket);
closesocket(clients[connectionsCount]);
WSACleanup();
return;
}
printf("Client acceptedn");
}
else
{
continue;
}
// Receive until the peer shuts down the connection
thread communicationThread(communicatingWithClient, &clients[connectionsCount++]);
communicationThread.join();
}
}
void communicatingWithClient(SOCKET *ClientSocket)
{
int iResult;
int iSendResult;
char recvbuf[DEFAULT_BUFLEN];
int recvbuflen = DEFAULT_BUFLEN;
do
{
printf("Receiving bytes from client...n");
iResult = recv(*ClientSocket, recvbuf, recvbuflen, 0);
if (iResult > 0)
{
printf("Bytes received: %dn", iResult);
// Echo the buffer back to the sender
iSendResult = send(*ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR)
{
printf("send failed: %dn", WSAGetLastError());
closesocket(*ClientSocket);
WSACleanup();
return;
}
printf("Bytes sent: %dn", iSendResult);
}
else if (iResult == 0)
{
printf("Connection closing...n");
}
else
{
printf("recv failed: %dn", WSAGetLastError());
closesocket(*ClientSocket);
WSACleanup();
return;
}
}
while (iResult > 0);
// shutdown the send half of the connection since no more data will be sent
iResult = shutdown(*ClientSocket, SD_SEND);
printf("ClientSocket shut downn");
if (iResult == SOCKET_ERROR)
{
printf("shutdown failed: %dn", WSAGetLastError());
closesocket(*ClientSocket);
WSACleanup();
return;
}
closesocket(*ClientSocket);
}

您的问题是由这部分引起的:

// Receive until the peer shuts down the connection
thread communicationThread(communicatingWithClient, &clients[connectionsCount++]);  
communicationThread.join();  

更准确地说,通过这个调用communicationThread.join((;如果等到完成与客户端的通信,您将无法与另一个客户端通信。(因为您必须等待通信线程完成才能创建新的通信线程(

因此,您的代码是连续

的multithreaind的用处是当线程被允许并行运行时。如果与每次创建线程立即同步,您将无法使用多线程,并且您的代码可以替换为顺序代码

为了获得最佳性能,您必须使用异步套接字,选择方法 (FD_WRITE, FD_READ, FD_CLOSE...(.如果每个套接字使用一个线程(同步(,那么如果您管理数千个连接,则线程之间迟早会产生开销,因为操作系统将浪费CPU/周期更改上下文(线程ID,堆栈变量等(而不是执行实际任务或工作。 你无法知道线程何时会做真正的工作,你不知道套接字何时会有数据(recv(,但你可以询问(选择(是否有数据挂起,并尽可能快地切换到同一线程中的另一个套接字。 另一方面,如果您有数据挂起,则其他套接字必须等待才能在同一线程的当前套接字中执行"recv"。 没有一个解决方案,取决于项目,但你可以创建一个线程池,一个线程可以处理十几个连接的套接字。如果您有更多的物理内核 (CPU(,则可以创建更多线程,但请注意逻辑内核,它们共享物理组件,并且性能可能会很差。

当您已经注册了项目的行为时,您可以优化或学习和改进它。所以,大数据+机器学习是下一步。

这是大多数框架的工作方式。