我正在尝试在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: %d\n", iResult);
WSACleanup();
return 1;
}
printf("WSA started\n");
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: %d\n", 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(): %d\n", WSAGetLastError());
freeaddrinfo(result);
WSACleanup();
return 1;
}
printf("ListenSocket created\n");
// Setup the TCP listening socket
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
if (iResult == SOCKET_ERROR)
{
printf("bind failed with error: %d\n", 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: %d\n", 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: %d\n", WSAGetLastError());
closesocket(*ListenSocket);
closesocket(clients[connectionsCount]);
WSACleanup();
return;
}
printf("Client accepted\n");
}
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: %d\n", iResult);
// Echo the buffer back to the sender
iSendResult = send(*ClientSocket, recvbuf, iResult, 0);
if (iSendResult == SOCKET_ERROR)
{
printf("send failed: %d\n", WSAGetLastError());
closesocket(*ClientSocket);
WSACleanup();
return;
}
printf("Bytes sent: %d\n", iSendResult);
}
else if (iResult == 0)
{
printf("Connection closing...\n");
}
else
{
printf("recv failed: %d\n", 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 down\n");
if (iResult == SOCKET_ERROR)
{
printf("shutdown failed: %d\n", 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的用途是允许线程在parallel中运行。如果您立即与每个创建同步,则将无法使用多线程,并且您的代码可能会被sequential
一个替换