I’m trying to make my small multi client server run in a pthread so that the clients may receive and send data to the database.
At the moment I can telnet to the server, send a message to the server and it will be echoed back. I’d like to take the clients input and “use” it and send an answer to him.
The only way I can accomplish that is if I put the server in a thread I assume.
So I created a simple thread and called the server function from it but the server wont start for some reason. What am I doing wrong?
void *startServer(void *)
{
cout << "Starting server\n";
Server();
}
int main()
{
pthread_t t;
pthread_create(&t, NULL, &startServer, NULL);
cout << "Hello";
return 0;
}
The only thing that shows after I run it is
Hello
You aren’t waiting for the thread, the main thread exits before the server thread can do anything, you need to do a pthread_join in the main thread, say, for example, after printing hello.