Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

question about writing threaded web server

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:


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?

Name: Anonymous 2009-04-21 0:50

(✌゚∀゚)☞ <VIPPER>

Name: Anonymous 2009-04-21 1:39

Why do you wait for two connections before processing one request?

Name: Anonymous 2009-04-21 1:47

No replies?

Name: Anonymous 2009-04-21 1:48

Oh oops, nevermind, forgot to reload.

>>3
huh?

Name: Anonymous 2009-04-21 1:48

Why do you wait for two replies before not noticing any?

Name: Anonymous 2009-04-21 1:51

>>5
    while(connfd = accept(sfd, NULL, NULL)
    accept( [appropriate args throughs *args] );

Name: Anonymous 2009-04-21 1:53

>>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.

Name: Anonymous 2009-04-21 2:25

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?

Name: Anonymous 2009-04-21 2:30

Read SICP.

Name: Anonymous 2009-04-21 2:42

>>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: Anonymous 2009-04-21 2:57

Here's a quarter. Buy yourself a better programming language.

http://www.red-bean.com/guile/guile/old/2217.html

Name: Anonymous 2009-04-21 3:17

>>12
that looks like some sort of scheme to me. i wouldn't invest a penny in it, much less a quarter.

Name: Anonymous 2009-04-21 3:17

>>12
o_o yes, sir.

Name: Anonymous 2009-04-21 3:27

>>13
...guile...
...that looks like some sort of scheme...

I bow to your obviously deep experience.

Name: Anonymous 2009-04-21 4:00

>>15
scheme as in:
A plot or secret, devious plan.

Name: Anonymous 2009-04-21 5:05

>>16
Just as well that was explicated. We wouldn't want anyone thinking you don't like SCHEME.

Name: Anonymous 2009-04-21 5:41

>>17
all the good schemes are free anyway.

Name: Anonymous 2009-04-21 5:56

>>12
s/http:\/\/www.red-bean.com\/guile\/guile\/old\/2217.html/http:\/\/erlang.org\/

Name: Anonymous 2009-04-21 6:03

>>19
What brain-damaged regex implementation doesn't let you specify the delimiter?

s@http://www.red-bean.com/guile/guile/old/2217.html@http://erlang.org/

Name: Anonymous 2009-04-21 6:21

>>20
Don't be angry at me, I just come here to hang out with you guys!

Name: Anonymous 2009-04-21 6:48

>>19
Not Patrick Volkerding approved, so fuck it.

Name: Anonymous 2009-04-21 6:54

Name: Anonymous 2009-04-21 8:30

>>22
Oh, Slackware! That was my first contact with Linux in 1996.

Name: Anonymous 2009-04-21 11:34

>>24
Newfag
And now I will go back to /b/, please

Name: Anonymous 2009-04-22 6:19

>>24
Same here. I recall the excitement of using something that wasn't DOS or Windows.

Name: Anonymous 2009-04-22 10:25

>>26
It wasn't exciting for me, because I had been using Macs since the mid-1980s.

Name: Anonymous 2009-04-23 6:47

hax my anux

Name: Anonymous 2009-04-23 11:05

>>27
BAHAHAAHAHAHAAHAHAHAAHAHAHAHA
faggot (。◕‿‿◕。)

Name: Anonymous 2013-01-19 23:44

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List