|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 ;-) |
|
#3
|
|||
|
|||
|
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/ |
|
#4
|
|||
|
|||
|
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 |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
Quote:
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. |
|
#7
|
|||
|
|||
|
ClayDowling, I can't find the cgi you're talking about on your site. Could you please point me to the exact page?
|
|
#8
|
|||
|
|||
|
The CGI in question is on the Newsreel Page.
|
|
#9
|
|||
|
|||
|
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; } |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > CGI with C/C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|