简体   繁体   中英

Program not entering while loop with only condition being 1

Hi I am writing a simple web server in C that will just handle simple get and post requests sent from a browser. I am not that familiar with C so debugging has been painful but I have gotten it to compile but continually I am getting a err_connection_reset response from my browser when trying to view webpage. I have narrowed it down to not entering the main loop that used to listen on the open socket but it will not enter it, here is my main function

int main(int argc, char *argv[])
{
    printf("in main");
    int portno;                 // port number passed as parameter
    portno = atoi(argv[1]);     // convert port num to integer

    if (portno < 24)
    {
        portno = portno + 2000;
    }
    else if ((portno > 24) && (portno < 1024))
    {
        portno = portno + 1000;
    }
    else
    {
        ;
    }

    // Signal SigCatcher to eliminate zombies
    // a zombie is a thread that has ended but must
    // be terminated to remove it
    signal(SIGCHLD, SigCatcher);

    int sockfd;                 // socket for binding
    int newsockfd;              // socket for this connection
    int clilen;                 // size of client address
    int pid;                    // PID of created thread
    // socket structures used in socket creation
    struct sockaddr_in serv_addr, cli_addr;

    // Create a socket for the connection
    sockfd = socket(AF_INET, SOCK_STREAM, 0);

    if (sockfd < 0)
    {
        error("ERROR opening socket\n");
    }

    // bzero zeroes buffers, zero the server address buffer
    bzero((char *)&serv_addr, sizeof(serv_addr));
    // set up the server address
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_addr.s_addr = INADDR_ANY;
    serv_addr.sin_port = htons(portno);

    // bind to the socket
    if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
        error("ERROR on binding. Did you forget to kill the last server?\n");

    listen(sockfd, 5);          // Listen on socket
    clilen = sizeof(cli_addr);  // size of client address

    while (1)
    {                           // loop forever listening on socket
        newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *) & clilen);

        if (newsockfd < 0)
            error("ERROR on accept");

        // Server waits on accept waiting for client request
        // When request received fork a new thread to handle it
        pid = fork();

        if (pid < 0)
            error("ERROR on fork\n");

        if (pid == 0)
        {                       // request received
            close(sockfd);
            // Create thread and socket to handle
            httpthread(newsockfd, cli_addr);
            exit(0);
        }
        else
            close(newsockfd);
    }                           /* end of while */

    return 0;                   /* we never get here */
}

The loop executes exactly once, and then the program terminates because you call exit(0) in the parent process. When you call exit, the entire process dies, including active threads. This includes the thread that is created by calling httpthread(newsockfd, cli_addr).

Besides, the child process is closing the socket. Check that you really want to do this, and do not terminate the program inside the parent process.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM