SunQuest
           C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old December 26th, 2002, 11:24 PM
David W David W is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Boston
Posts: 2 David W User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old December 27th, 2002, 06:45 AM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 6
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 ;-)

Reply With Quote
  #3  
Old December 27th, 2002, 10:54 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
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/

Reply With Quote
  #4  
Old December 27th, 2002, 11:07 AM
David W David W is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Boston
Posts: 2 David W User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old December 27th, 2002, 11:16 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
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.

Reply With Quote
  #6  
Old December 27th, 2002, 11:39 AM
Wingman Wingman is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2002
Location: Bavaria, Germany
Posts: 140 Wingman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 40 m 41 sec
Reputation Power: 6
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.

Reply With Quote
  #7  
Old December 28th, 2002, 02:49 PM
poring poring is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Posts: 68 poring User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 39 m 9 sec
Reputation Power: 7
ClayDowling, I can't find the cgi you're talking about on your site. Could you please point me to the exact page?

Reply With Quote
  #8  
Old December 30th, 2002, 09:08 AM
ClayDowling ClayDowling is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Flint, MI
Posts: 328 ClayDowling User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 19 m 25 sec
Reputation Power: 6
The CGI in question is on the Newsreel Page.

Reply With Quote
  #9  
Old March 7th, 2003, 03:13 AM
operator smooth operator smooth is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 61 operator smooth User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 6
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;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > CGI with C/C++


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway