int stopServer(Server * srv) { //turn off the server servOff(srv); //join with the server thread (wait for it to die) //discard its output pthread_join(srv->serverThread, NULL); return 1; }
int startServer(Server * srv) { //tell the server it's on, this has to be done first because //the server thread checks for it servOn(srv); srv->serverThread = makeThread(serverThread, srv); //if the server thread has not been made if(!(srv->serverThread)) { //turn off the server power flag servOff(srv); return 0; } //server is now running, return success else return 1; }
Server * makeServer(unsigned short port) { //start with the empty server Server * srv = NULL; srv = malloc(sizeof(srv)); if(!srv) return NULL; servOff(srv); srv->socket = listenOnPort(port); srv->port = port; srv->errOut = stderr; srv->serverThread = (pthread_t) 0; if(!(srv->socket)) return NULL; return srv; }
void * serverThread(void * args) { Server * me = (Server *) args; //listen on the given port if(listen(me->socket, me->queueLen) == -1) { perror("listen"); servOff(me); return NULL; } //all clear, run the main loop function while(servStatus(me)) { DEBUGOUT("calling servLoop()"); servLoop(me); } delConnections(me); return NULL; }