Name: Anonymous 2009-04-21 0:42
I'm trying to teach myself threads and sockets in C and decided the best way was to write a multithreaded http server. I'm having a little bit of trouble putting together the pieces, can someone more knowledgeable than myself tell me whether I have the right idea?
I've figured out how to open and bind a socket. Here's what I'm trying to do after that:
Is the general idea right?
I've figured out how to open and bind a socket. Here's what I'm trying to do after that:
int main() {
// set up connection
...
// already listening, socket connection var is sfd
while(connfd = accept(sfd, NULL, NULL)
{
if(connfd < 0)
// scream and die
pthread_create( [ vars, handle thread with serv() ] );
}
}
void* serve(void* args)
{
/// declare vars used below
accept( [appropriate args throughs *args] );
recv( [from connection[ );
// *request now contains the URL the client asked for
send( [http 200 or 404, depending] );
// mutex lock file pointer to log file, to log pages served
// write to log file
// mutex unlock log file
pthread_exit([whatever arg it is]);
}Is the general idea right?