Ok, so I've just kept working at it. So tired now, but oh so close. Stuck again though. I have the server creating threads now, and I have each socket kept track of in a class instance which in turn is in a vector to simplicity of managing all clients easily.
But, now ... How do I listen for incoming messages on each client properly? Do I just have an infinite (well almost) loop in the threadproc checking for data? Is this acceptable?
Also, perhaps someone can answer me this (I think it may have something to do with critical data or something I dunno anything about it really.
Code:
DWORD WINAPI listenroutine(){
cout << "Listener thread started\n";
for(;;){
if(client.keeplistening){
char *data = client.recievedata();
if(data != NULL){
cout << "Data recieved: " << data << endl;
cout << "Resizing strmsgs\n";
char *tmp = (char*)malloc(sizeof(strmsgs));
strcpy(tmp, strmsgs);
strmsgs = (char*)realloc(strmsgs, sizeof(tmp)+sizeof(data)+1);//+1 for safety
strcpy(strmsgs, tmp);
cout << "concat a '\\n' to strmsgs \n";
strcat(strmsgs, "\n");
cout << "--- OK!\nconcat data to strmsgs\n";
strcat(strmsgs, data);
cout << "--- OK!\n";
SetWindowText(msgsbox,strmsgs);
UpdateWindow(msgsbox);
cout << client.keeplistening << endl;
}else{
cout << "data: " << data << endl;
}
}else{
SetEvent(hEvent);
}
}
cout << "Listener thread ended\n";
return 0;
}
So, client is a class in global scope. If in my wndproc the window closes client.keepclosing = false, however this never seems to do anything in the threadproc.. I stuck that event in there to make the program wait for it to finish up before exiting as I was getting a segfault. I hope that was the correct thing to do. But I've never touched threads so I dunno once again.
Any help at all would be awesome