1

My IDE

Sorry if this question is a duplicate. I tried to look over stackexchange before posting this (and I couldn't find anyone who was having a similar issue). I am having trouble getting my server to do anything other than responding with "Stopping server". I'm not too sure what I'm doing wrong. I don't want to post my code online unless I absolutely have to because I know the CS50 guidelines say not to. I think I have fully implemented my code but I was not able to test

By the way- my issue isn't with starting the Staff Server. I believe I did have trouble with that a few days ago but I'm not sure what finally solved that problem for me.

Thanks!

5

The Stopping server message comes from the stop() function defined here:

/**
 * Stop server, deallocating any resources.
 */
void stop(void)
{
    // preserve errno across this function's library calls
    int errsv = errno;

    // announce stop
    printf("\033[33m");
    printf("Stopping server\n");
    printf("\033[39m");

    // free root, which was allocated by realpath
    if (root != NULL)
    {
        free(root);
    }

    // close server socket
    if (sfd != -1)
    {
        close(sfd);
    }

    // stop server
    exit(errsv);
}

This function is called at seven other places in the code.

In main():

    // check for control-c
    if (signaled)
    {
        stop();
    }

The other six calls to stop() are all part of the start() function, which is worth a close reading even though you don't have to write any part of it. There are six different ways that the start() function can fail, and if any of them occur, the stop() function is called so that the server will fail gracefully and free() any memory that has been allocated.

The first two tests are particularly important to understand:

// path to server's root
root = realpath(path, NULL);
if (root == NULL)
{
    stop();
}

// ensure root is executable
if (access(root, X_OK) == -1)
{
    stop();
}

What this means is that the server's root must exist (i.e., you cannot just start ./server asdf;lksdfha;sdlfkhdlkfh;l and expect it to work), and that the server's root must be a directory with the executable bit set (i.e., you cannot set the root directory to a file, as by starting ./server ~/workspace/pset6/public/cat.html and expect it to work).

Furthermore, you need to understand that the full /path/to/root may be specified as either an absolute or relative pathname.

If you have set your current working directory to the pset directory via cd ~/workspace/pset6/, then you can simply start ./server public and it should work with the relative pathname, since ./public is in the current directory.

But if you start the pathname with a slash (/) character, that's an absolute pathname and needs to be specified all the way from the root directory, like so: ./server /home/ubuntu/workspace/pset6/public.

If this answers your question, be sure to accept my answer by clicking on the green check mark. Otherwise, the forum will continue to list this as a zombie unanswered question, and it will haunt the lists of unanswered questions forever!

  • 1
    re: "and that the server's root must be a directory with the executable bit set ". Discovered yesterday that ./server server will in fact start server. It won't do much, mind you :) From this post – DinoCoderSaurus Aug 10 '16 at 11:15
  • @DinoCoderSaurus: Ha! I stand corrected. I guess you can start server.c with a file path as its root, rather than a directory. But clearly, it's better to use it as intended, and serve a whole directory rather than just one file! – Matt Obert Aug 10 '16 at 11:29
  • 1
    Hear, hear! At that point it's "operator error". Why doesn't my "@Matt Obert" work. Oy! – DinoCoderSaurus Aug 10 '16 at 11:40
  • 1
    Thank you very much for the detailed response! Really helped me out a lot – Sid Panjwani Aug 10 '16 at 18:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .