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:
int main() {
// set up connection
...
// already listening, socket connection var is sfd
while(connfd = accept(sfd, NULL, NULL)
{
if(connfd < 0)
// scream and die
>>7
hurrr yeah okay I'll fix it, I just typed that into here and made a mistake, is the general idea correct? I'm still trying to get the various socket / pthread arguments right.
HO I got it working! Right now the server creates a new thread for each HTTP request it gets and prints it to console (will expand it later)
I have one last question: I'm listening for connections in an infinite loop, so the only way right now to end the "server" is to hit ctrl+c in the console it's running (haven't backgrounded it) but when i do that and try to restart it, the port is still binded.
Do I have to set up a SIGTERM listener that close()s the socket or is there an easier way?
>>9
Bound sockets will die after a period of time (WAIT_CLOSE) even if you close them explicitly. This is to "catch" any residual packets that might still be on the network after the socket is closed, to avoid the situation of another program binding to the same port and receiving unsolicited data.
Name:
Anonymous2009-04-21 2:57
Here's a quarter. Buy yourself a better programming language.