   OO Design- WinSock Mutithreaded Server problem
  I am currently working through object orientating?! the article WinSock2 for Games In the tutorial it uses a function called startThread() for the accepting of clients (i understand this is pseudocode for createThread()) - so, as I'm using C++ I understand I need a workaround as threads are C, Encapsulating Win32 threads in C++ I have implemented the Thread class as described and renamed my accept clients routine to process() :void ServerSocket::process(){while (numClients < MAX_CLIENTS) // let MAX_CLIENTS connect{// accept a connectionclient[numClients] = accept (mySocket, (struct sockaddr*)&from[numClients], &fromlen); if (client[numClients] == INVALID_SOCKET){ // error accepting connectionWSACleanup ();return;}else{ // client connected successfully// start a thread that will communicate with clientcreateThread();numClients++;}}}But when I compile I get the error:Quote:error C3861: 'createThread': identifier not found I have included the class:#include <iostream>#include <winsock2.h>#include <stdio.h>#include "Thread.h"using namespace std;Because this is an abstract class I am not putting it as a member of my Socket class is this correct?Sorry about the lengthy introduction [wow]   
  Apparently, the createThread function cannot be found.Quote:as threads are CThreads are OS-specific. Right now, neither C nor C++ as languages support them.But as far as C++ goes, boost::thread has been accepted into upcoming language update, so it should be treated as standard C++ thread implementation, and will be made available as part of standard library in future versions, or it might already be available on some existing compilers.I would strongly suggest to use that.   ;  Thanks for the fast reply I shall read through the notes. Have you used this tool before?   ;  If you are on Windows, take a look at the Win32 API function ::CreateThread(). It's documented on MSDN. If you're on a *NIX derivative, try pthread_create().   ;  hplus0603 as i explained in the op unfortunately this does not work in c++, there is a workaround but this is causing a problem.   ;Quote:this does not work in c++, Given that what I suggested is what pretty much all threaded C++ programs do, I believe you should learn to program before you blame the language :-)I assume you mean that you can't pass an instance member function as a function pointer, and you have to trampoline across a static member function? Here's some code I wrote last week, which works just fine:class CAsyncWorker{  public:    CAsyncWorker();    static DWORD CALLBACK ThreadEntry(void *ptr);    void Work();    DWORD threadId_;    HANDLE thread_;    HANDLE workAvailable_;    volatile bool running_;  ...};CAsyncWorker::CAsyncWorker(){  running_ = true;  workAvailable_ = ::CreateEvent(0, false, false, 0);  thread_ = ::CreateThread(0, 32768, &CAsyncWorker::ThreadEntry, this, 0, &threadId_);}DWORD WINAPI CAsyncWorker::ThreadEntry(void *ptr){  ((CAsyncWorker *)ptr)->Work();  return 0;}void CAsyncWorker::Work(){   while (true)  {    ::WaitForSingleObject(workAvailable_, INFINITE);    if (!running_) break;    CWork *cw = 0;  ...Note the declaration of ThreadEntry as a static DWORD CALLBACK function. If you forget one of those, you'll either end up with a compiler error, or (if you cast wrong), you'll end up with a mis-matched calling convention and a SegV.;  hplus0603 I have never done anything with multiplayer or multithreading before so this is a double whammy for me - that coupled with not being a terribly good c++ makes a bad match.I have made a few advances with this program but now i'm crashing out with an unhandled exception.So here is what I am aiming to achieve:->In my accept function I want to keep accepting clients until I have reached MAX_CLIENTS.->Once a client has connected I want to start a thread process for each client. This process will handle receiving data and sending a simple acknowledgment.So here is my accept() as it is now:void ServerSocket::Accept(){while (numClients < MAX_CLIENTS) // let MAX_CLIENTS connect{// accept a connectionclient[numClients] = accept (mySocket, (struct sockaddr*)&from[numClients], &fromlen); if (client[numClients] == INVALID_SOCKET){ // error accepting connectioncout << "Error:  Unable to accept connection!\n";WSACleanup ();return;}else{ // client connected successfully// start a thread that will communicate with clientcout << "Player joined!\n";threads[numClients]->createThread();numClients++;}}}threads is a member of my socket class defined Thread *threads[MAX_CLIENTS]; (this could be where my understanding goes wrong). I thought Thread *threads[MAX_CLIENTS] should be Thread threads[MAX_CLIENTS], but I get the error:Quote:error C2259: 'Thread' : cannot instantiate abstract class And here is my thread class:struct workAround {Thread* thisThread;};// pass a workAround struct instead of thisHANDLE Thread::createThread(){workAround* wA= new workAround;wA->thisThread= this;return ::CreateThread(NULL, 0, (unsigned long (__stdcall *)(void *))this->runProcess, (void *)wA, 0, NULL);}// static methodint Thread::runProcess(void* pThis){workAround* wA= (workAround*)pThis;// this will call the appropriate method, as Process is a virtual method Thread* thread = wA->thisThread;delete wA;return thread->process();}at the moment my process() simply prints to console a thread has been created.I would be grateful if you could point out where I'm going wrong because I cant see it myself [bawling]Also, hplus0603 thanks for your code but i'm really struggling to understand it now that I have familiarized myself with my own thread class. Maybe if you could explain what it does it could help me a little. Kind regards.;  Problem solved   ;  Hey guys sorry to bring this back up again but I have another problem - although this time it is more general programming than multiplayer.So here's where I'm at:-> Server starts and enters a loop to add players to the game lobby-> For each new player a thread is created-> In the thread the players name is retrieved and added to the lobby. This is handled in a loop that doesn't exit until the connection is closed or the lobby is full.void clientThread( void* clientSock ){DataSocket* dsock = new DataSocket((SOCKET) clientSock);// buffer to hold recived datachar buff[STRLEN];int connectionOpen = 1;// Loop as long as lobby isnt fullwhile(notFull && connectionOpen){if(dsock->recvData(buff, STRLEN) != 0){myLobby.addPlayer(buff);}else{cout << "Connection Closed" << endl;dsock->closeConnection();connectionOpen = 0;myLobby.removePlayer();}}}The problem lies at the end of this loop. I get:Quote:Run-Time Check Failure #2 - Stack around the variable 'buff' was corrupted.Can anyone shed some light on this problem?Kind regards   ;Quote:Original post by RenderMeQuote:Run-Time Check Failure #2 - Stack around the variable 'buff' was corrupted.Can anyone shed some light on this problem?"dsock->recvData(buff, STRLEN)" is writing beyond the end of buff.Another possibility is that addPlayer expects a valid C string, but the contents of buff are not zero terminated, either because \0 wasn't received properly, or because it wasn't sent over network at all.You also have a memory leak, since you never seem to free the DataSocket.   