Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

CGI in C Basics

CGI in C Basics

CGI in C Basics

So I'm trying to teach myself CGI programming. (I'm trying to do it in C because I know the language fairly well as opposed to other scripting languages which I do not.) I was trying to go through a tutorial:


I want to process simple form data (an idea I want to eventually implement at work). I've been trying to do all this from my hard drive. I have the executable in my compiler folder and the HTML file in another folder.

From what I gathered, I'll need to be able to process the "QUERY_STRING" variable, but it never seems to get set up properly. I try checking it with getenv("QUERY_STRING") but it always comes up NULL.

The example file I've been practicing with has the following segment for the FORM element:

<FORM METHOD="POST" ACTION="/cgi-bin/usr-inf">

Given that I was working on this in Windows, I changed it to:

<FORM METHOD="POST" ACTION="FILE://C:\<my_compiler_path>/hello.exe">

but Firefox keeps asking me where I want to save the file instead of running it. Understandable for security reason but still... (I originally had it as HTTP instead of FILE, but I kept getting "server not found" errors.)
The same things happens when I copied, pasted, and recompiled these files to my Ubuntu netbook.

The "hello.exe" is simply the "Hello" program from the tutorial page.

How can you simulate client/server interaction on a PC? I'm also surprised that CGI output is supposed to be written to standard-out, so of course I see the output in a command prompt window instead of a browser.

RE: CGI in C Basics


Quote (AbidingDude)

<FORM METHOD="POST" ACTION="FILE://C:\<my_compiler_path>/hello.exe">
That looks bad. CGI script or program is an executable, so something needs to set up the environment, start the executable, pass it input and sends its output back to the one which made the request. That will not happen when using file:// protocol, only on http://. And to handle that, you need a web server.

So first install a web server, configure it to run CGI, make sure hello.exe is found according to the configuration when needed and after that your program will get the QUERY_STRING. ( Though will be empty string in case of your above example. )


RE: CGI in C Basics


Quote (feherke)

That looks bad. CGI script or program is an executable, so something needs to set up the environment, start the executable, pass it input and sends its output back to the one which made the request. That will not happen when using file:// protocol, only on http://. And to handle that, you need a web server.

Yeah it does look bad bigears but it's only because I have no idea what I'm doing...

When you say, install a web server. Do you mean obtain (or build) another PC and turn it into a server? Sounds kinda fun actually, but I'm not sure about the finances or the physical space.

RE: CGI in C Basics


Absolutely not. Here server means service ( Windows terminology ) or daemon ( Unix terminology ). In other words, install a program capable to handle incoming HTTP requests :
  • The simplest for Windows in my opinion is Abyss Web Server. The free for personal use edition will be fine for your experiments.
  • Not sure about Windows software distribution details, but as far as I know, Microsoft's own web server, IIS, is included in some editions, so maybe you already have it installed. IIS being more popular, we have forum41: Microsoft: Internet Information Server for it.
  • Or you can pick one from the Comparison of web server software Wikipedia article, as long it can run on Windows and supports CGI.
  • While I worked on Windows, I used Lighttpd run in Cygwin. ( Not what I would recommend for a beginner, just to emphasize that it may be a wast variety of software. )
When the web server is running, it accepts incoming requests on certain ports ( by default 80 ). So if your machine has direct connection to the internet, make sure the firewall rejects any incoming request from outside. Of course, is enough to ask the web server to only listen on the loopback address ( IP address :, hostname : localhost ), as that is unique to every machine and can not be addressed from elsewhere. So the URL to your program will be something like http://localhost/cgi-bin/hello.exe .

By the way, multiple web servers may run on the same machine as long as they are configured to listen for incoming requests on different ports. So while experiencing, you may even install more web servers that you find attractive, try them all, then decide later which one ( or more ) to keep.

Regardless the language you use to write you program, your main problem in this moment seems to be the lack of CGI knowledge, for which forum452: CGI is more appropriate.


RE: CGI in C Basics

Thank you for breaking that down.

I just installed Abyss. It looks like I'll have to take some time to learn it.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close