I have a WinSock server and I was wondering how to make it multi-threaded. I have used threads before and I understand what they do. But I'm not sure how to use them with my server code. So here it is:
main.cpp
/* This is our Server */
//Includes
#include "Functions.h"
//Main function
int main(){
DLLVERSION = MAKEWORD(2,1);
answer = WSAStartup(DLLVERSION, &wsaData);
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet_addr(HostIP);
addr.sin_family = AF_INET;
addr.sin_port = htons(PORT);
sListen = socket(AF_INET, SOCK_STREAM, NULL);
bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
listen(sListen, SOMAXCONN);
//Listening for clients
for(;;){
Print("Waiting for incoming connections");
//If a connection is found
if(sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen)){
Print("Connection found!");
Send("Hello!");
}
}
}
How would I make this server multithreaded?
I tried using CreateThread();
but as soon as another thread is created, the last thread ends for some reason. Can anyone help?