1

I've written a server that when a the browser tries to connect to certain sites it checks a blacklist and sends back a 404, however when I call send() there is no error but the message does not appear on the web browser unless I close the connection?

Any advice?

Accepting connection from browser

while(1){
    connfd = accept(fd, (struct sockaddr *) &cliaddr, &cliaddrlen);
    if (connfd == -1) {
        perror ("unable to accept");
        return 1;
    }
    printf("%s:%d connected\n", inet_ntoa(cliaddr.sin_addr), ntohs(cliaddr.sin_port));
    printf("%d",threadIndex);
    pthread_create(&(thread[threadIndex++]), NULL, processRequests, (void *)connfd);

    }

Process Requests method send snippet:

if(blacklisted ==1){
                    printf("is blacklisted\n");
                    char *response404 = "HTTP:/1.1 404 not avaliable\r\n\r\n";

                    printf("%s\n",response404);
                    int len, bytes_sent;
                    len = strlen(response404);
                    bytes_sent = send(connfd, response404, len, 0);
                    if(len != bytes_sent){
                        perror("message length doesn't match");

                    }
            }
5
  • 2
    Check on line 42 whether you mean i rather than j. Commented Feb 26, 2012 at 23:55
  • Shouldn't each thread get its own copy of connfd? It looks like in your code they're all sharing the same variable. Commented Feb 27, 2012 at 0:07
  • try adding either a "content-length: 0" to your response or another \r\n. It doesnt show until you close the connection because its still waiting for the message content. Commented Feb 27, 2012 at 0:14
  • Looking at other examples I think the way I'm handling the connfd and threads is correct? Commented Feb 27, 2012 at 0:16
  • I tried adding those, didnt seem to work Commented Feb 27, 2012 at 0:30

1 Answer 1

3

Browser is waiting for the connection to be closed before it parses and shows the page.

There is no way around.

in HTTP/0.9 and 1.0, the connection is closed after a single request/response pair.

In HTTP/1.1 a keep-alive-mechanism was introduced, where a connection could be reused for more than one request.

Note: Keepalive technique has no use here because you are sending one single page for every client connecting to certain location.

Sign up to request clarification or add additional context in comments.

4 Comments

so the connection has to be closed and reopened for every request/response pair?
Everytime you send something to the browser, you must close the connection afterwards so the browser knows when the server side is done sending all the data to be parsed in the browser. So basically you must create a loop which handles the opening and closing the tcp connection.
Thank you, that worked :) although when I try to create a new thread I'm getting a segfault, is my thread creation loop correct?
@drunkmonkey unfortunately I have no real experience on threaded programming so my suggestion is to create another question or google for some nice thread tutorials.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.