The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
CGI with C/C++
Discuss CGI with C/C++ in the C Programming forum on Dev Shed. CGI with C/C++ C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 26th, 2002, 11:24 PM
|
|
Junior Member
|
|
Join Date: Dec 2002
Location: Boston
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
CGI with C/C++
I'd like to take a program I have written in C and port it to the web. Assume that the input for the script will be a list of 4 integers (call the variables whatever you want -- it doesn't matter).
My idea is to create a form whereby the user can input the integers (heck, even with one textbox they can do 2,3,4,5 etc), and then press the submit button to execute the CGI script (a c program). The C program will then perform the calculations, and generate a new html page with the answers.
Anybody have any ideas where I could find the info to start this process?
Would I have script.cgi, and then have the complied c program "numbers.c"? If so, does the executable C file have to point to the program executable (e.g. /usr/bin like in perl)?
Also, because it's just integers and some really funky calculations, I have no problem using a GET method. I think it'd be easier to read the variables into the C program this ways as opposed to decoding the POST method. It is REALLY that bad to use a GET method -- especially considering that the data here is not sensitive in any way?
Thanks in advance,
David W
|

December 27th, 2002, 06:45 AM
|
|
Contributing User
|
|
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 11
|
|
|
If your apache (assuming you use apache) has user defined cgi-bin's turned on, you just need to copy your bin into your ~/wwwroot/cgi-bin/ or ~/public_html/cgi-bin/ (or if you're root to /usr/local/apache/cgi-bin/) directory and change it to a+rx. That's it :-).
All files which are in cgi-bin are executed and it's stdout output will be sent to the client. You can use .sh files, .tcl, .pl, .whatever if it's just executable.
You can get all request variables thorugh the enviroment of your script/executable/whatever, just do a sample request and print out all env variables ;-)
|

December 27th, 2002, 10:54 AM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
|
CGI with C
David,
Writing CGI applications with C is pretty easy. I have source for a moderately complex CGI at My Website
I also discuss the topic at Lazarus Notes
__________________
Clay Dowling
Lazarus Notes
Articles and commentary on web development
http://www.lazarusid.com/notes/
|

December 27th, 2002, 11:07 AM
|
|
Junior Member
|
|
Join Date: Dec 2002
Location: Boston
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
What do you think about the GET vs POST thing above (e.g. using GET for really simple, not security-minded things)? I don't feel like writing a script to decode the POST method.
Thanks,
DavidW
|

December 27th, 2002, 11:16 AM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
David,
Read this article: Who's Afraid of the Big Bad C
Among other things, it mentions a nice interface library for the CGI standard. The source code I mentioned in a previous post also provides examples.
Wingman's advice is correct, but it's the hard way around.
|

December 27th, 2002, 11:39 AM
|
|
Contributing User
|
|
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 11
|
|
Quote:
What do you think about the GET vs POST thing above (e.g. using GET for really simple, not security-minded things)? I don't feel like writing a script to decode the POST method. |
There isn't much difference between GET and POST, beside size limitation and that you can't read it in the URL (and you can't transfer file types via GET).
if you use <form method="get" /> then *all* your input variables are in your GET field. If you use post, then your input variables are in the body of your http request.
Example:
Code:
<form method="" action="http://localhost:81/test">
<input type="hidden" name="key1" value="value1" />
<input type="hidden" name="key2" value="value2" />
<input type="submit" />
</form>
If you click "submit" with "get", your client will send the following request to your server:
Code:
GET /test?key1=value1&key2=value2 HTTP/1.1
Host: localhost:81
...and with post...
Code:
POST /test HTTP/1.1
Host: localhost:81
Content-Length: 23
key1=value1&key2=value2
The decoding routine is in both cases the same, split into & pairs and then into key, value pairs with delimiter = and last the decoding.
If you do file uploading, then your post body will be somehow complicated since it's diverted into several bodies.
See? There isn't really a difference between both :-)
But ClayDowling is right, those decoding routines and nicer accessing of HTTP variables has written a lot times before, so just search for a CGI request/response wrapper for c/c++ :-).
Last edited by Wingman : December 27th, 2002 at 11:45 AM.
|

December 28th, 2002, 02:49 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Posts: 68
Time spent in forums: 5 h 39 m 9 sec
Reputation Power: 12
|
|
|
ClayDowling, I can't find the cgi you're talking about on your site. Could you please point me to the exact page?
|

December 30th, 2002, 09:08 AM
|
|
Contributing User
|
|
Join Date: Oct 2002
Location: Flint, MI
Posts: 328
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 11
|
|
|

March 7th, 2003, 03:13 AM
|
|
Contributing User
|
|
Join Date: Mar 2003
Posts: 61
Time spent in forums: < 1 sec
Reputation Power: 11
|
|
|
CGI programs in C are as easy to write as in PERL or any other language.
The basic idea of CGI is that input is taken from envirement variables and standard input, while output is printed to standard output.
Simple example:
#include <stdio.h>
int main(void)
{
int i;
puts("Content-Type: text/html\n");
for (i=1; i<=5; i++)
printf("<h%d>Hello!</h%d>\n", i, i);
return 0;
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|