I've been trying for hours to create a server which accepts multiple connections, assigns a class for each connection, and can use them later to send data to each one.
What I have so far:
ClientConnection.cpp
#include "stdafx.h" #include "ClientConnection.h" ClientConnection::ClientConnection() { } ClientConnection::~ClientConnection() { }
ClientConnection.h
#pragma once class ClientConnection { public: ClientConnection(); ~ClientConnection(); };
GameServer.cpp
#include "stdafx.h" #include <windows.h> #include <stdlib.h> #include <stdio.h> #include <winsock.h> #include <vector> #include "GameServer.h" int port = 1123; GameServer::GameServer() { WSADATA wsaData; sockaddr_in server; SOCKET listenSocket; DWORD clientConnection; // start highest version of winsock if (WSAStartup(0x101, &wsaData) != 0) return; // fill winsock struct server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(port); // create listen socket listenSocket = socket(AF_INET, SOCK_STREAM, 0); if (listenSocket == INVALID_SOCKET) return; // bind socket to port if (bind(listenSocket, (sockaddr*)&server, sizeof(server)) != 0) return; // listen for a connection if (listen(listenSocket, 5) != 0) return; SOCKET clientSocket; sockaddr_in from; int fromlen = sizeof(from); while (true) { // accept connections clientSocket = accept(listenSocket, (struct sockaddr*)&from, &fromlen); printf("Client connected\r\n"); // create new thread and pass socket CreateThread(NULL, 0, ListenThread, (LPVOID)clientSocket, 0, &clientConnection); } // shutdown winsock closesocket(listenSocket); WSACleanup(); } GameServer::~GameServer() { } DWORD WINAPI GameServer::ListenThread(LPVOID socket) { ... }
GameServer.h
#pragma once class GameServer { public: GameServer(); ~GameServer(); private: DWORD WINAPI ListenThread(LPVOID socket); };
On this line,
CreateThread(NULL, 0, ListenThread, (LPVOID)clientSocket, 0, &clientConnection);
I'm getting the errors:
'GameServer::ListenThread': non-standard syntax; use '&' to create a pointer to member argument of type "DWORD (__stdcall GameServer::*)(LPVOID socket)" is incompatible with parameter of type "LPTHREAD_START_ROUTINE"