Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8954205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:16:07+00:00 2026-06-15T14:16:07+00:00

I write a very very simple HTTP server to call a simple CGI program

  • 0

I write a very very simple HTTP server to call a simple CGI program which I wrote,
but I the HTTP server can’t show the result of CGI program on my browser.
The part code of my HTTP server is in following.

while(1)
{
    struct sockaddr_in client_addr;
    int addrlen = sizeof(client_addr);

    /*Accept*/
    if((clientfd = accept(sockfd, (struct sockaddr *)&client_addr, (socklen_t*)&addrlen)) < 0)
    {
        perror("Accpet Error");
        close(sockfd);
        exit(-1);
    }
    else
    {
        /*Fork process*/
        if((child = fork()) < 0)
        {
            perror("Fork Error");
            close(sockfd);
            exit(-1);
        }
        else if(child == 0)
        {       
            int fd[2]; //pipe a fd
            close(sockfd); 
            setvbuf(stdin, NULL, _IONBF, 0);
            setvbuf(stdout, NULL, _IONBF, 0);
            setvbuf(stderr, NULL, _IONBF, 0);   
            buffer1.clear();
            buffer1.resize(VECTOR_SIZE);
            while(1)
            {
                if((res = recv(clientfd, buffer1.data(), buffer1.size(), 0)) > 0)
                {

                    string token = "QUERY_STRING=" + buffer1.data();
                    pipe(fd);
                    dup2(fd[1], STDOUT_FILENO);
                    close(fd[0]);
                    execlp("cgi", "cgi", NULL);
                     //if the bug fixed, will use `execvpe` here
                    cout << "fail" << endl;
                }


            }
        }
        else    
        {
            //exit(0);
            close(clientfd);
        }
    }

}   

And, my CGI program just
string reply = "HTTP/1.1 200 OK\nTEST"; cout << reply;
But the browser can’t show the result and timeout.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:16:08+00:00Added an answer on June 15, 2026 at 2:16 pm

    You need 2 CRLFs between your HTTP Response and the content. Additionally, most browsers will probably not display the content without at least setting the Content-Type header. For your purposes, you should just set Content-Type: text/plain testing purposes. If you intend to serve other mime types, you will want to account for that in your CGI.

    As you’ll probably hear from others, they’ll be curious why you are attempting this … i’ll chalk it up to an academic exercise or just curiousness … but one thing you’ll want to do is put the responsibility for returning the response code and status on the HTTP server itself, not the CGI.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a very simple script , which takes a process name
I am trying to write a very simple RSS channel which would display the
I'm trying to write a very simple image processing program for fun and practice.
I'm trying to write a very simple program, I want to print out the
For fun I'm trying to write a very simple server in C. When I
This is a very simple game I'm trying to write but I have a
I want to create a simple http proxy server that does some very basic
I'm working though [http://llvm.org/docs/WritingAnLLVMPass.html][1], trying to write a very simple pass. I've written the
I am trying to write a very simple thunk that can be used from
I had found written in python, a very simple http server, it's do_get method

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.