+ -
当前位置:首页 → 问答吧 → 100分求助:如何使用c/c++开发CGI?

100分求助:如何使用c/c++开发CGI?

时间:2007-03-05

来源:互联网

我是初学者,了解c/c++,想开发CGI,请问使用VC.net可以开发么?具体要建立什么样的工程?怎样编译?谢谢!最好能给个hello   world.谢谢!

作者: lyderek2005   发布时间: 2007-03-05

我初学:http://groups.google.com/group/w-w-w
C写的处理method=get表单的cgi程序  
 
index.html的大致内容:

  11           <table   border= "0 ">
  12           <tr> <td   width= "350 "> </td> <td   width= "350 "> </td> <td
width= "350 "> </td> </tr>
  13           <!--             序章-死灵法师       -->
  14           <tr> <td   colspan= "3 "
align= "center "> 序章-死灵法师 </td> </tr>
  15           <tr>
  16           <td   width= "33% "   align= "center ">
  17                   <button   type= "submit "   name= "filename "
value= "000_001 "> 序章上   前夜 </button>

main.c

    1   #include   <stdio.h>
    2   #include   <stdlib.h>
    3   #include   <errno.h>
    4
    5   #define   BUF_N       512
    6   #define   PATH         "../xiedu/ "
    7
    8   static   inline   void
    9   begin(void)
  10   {
  11           fprintf(stdout, "Content-type:text/html\n\n ");
  12           fprintf(stdout, " <html> <title> 亵渎 </title> \n ");
  13           return;
  14   }
  15   static   inline   void
  16   end(void)
  17   {
  18           fprintf(stdout, " </html> ");
  19           return;
  20   }
  21   static   void
  22   content(const   char   *   const   path)
  23   {
  24           FILE   *   fp   =   fopen(path, "rb ");
  25           char   *   buf   =   NULL;
  26           if(   NULL   ==   fp   ){
  27                   fprintf(stdout, "%d:打开错误 </br> \n ",__LINE__);
  39           free(buf);
  40           fclose(fp);
  41           return;
  42   }
  43   int
  44   main(int   argc,char   **argv)
  45   {
  46           int   i,n=0;
  47           begin();
  48           /*         method   =   get   */
  49           if(   getenv( "QUERY_STRING "   )   ){
  50                   char   *t=NULL;
  51                   if(   NULL   ==   (t=(char   *)malloc(sizeof(char)*BUF_N))   ){
  52                           end();
  53                           exit(-1);
  54                   }
  55                   sprintf(t, "%s ",PATH);
  56                   strcat(t,strstr((char   *)getenv( "QUERY_STRING "), "= ")+1);
  57                   /*
  58                   fprintf(stdout, "打开文件:%s </br> \n ",t);
  59                   */
  60                   content(t);
  61                   free(t);
  62           }
  63           end();
  64           return   0;
  65   }

作者: avalonBBS   发布时间: 2007-03-05

参考:   http://www.eybuild.com

作者: newzy   发布时间: 2007-03-07

c/c++   和   VC.net   是两回事了!

作者: yagas   发布时间: 2007-03-12

去研究一下框架的概念吧,开发程序没框架怎么行

作者: caoyongang   发布时间: 2007-03-14

嗯   就是~

作者: Red_angelX   发布时间: 2007-03-15

等待牛人来答.

作者: knowledge_Is_Life   发布时间: 2008-05-01


【1】配置你的服务器
以 apache为例:
修改apache的配置文件httpd.conf:
<Directory "。。。。。。。。。。">
    AllowOverride None
    Options None
    Order allow,deny
    Allow from all
</Directory>
#AddHandler cgi-script .cgi

改为:
<Directory "D:/Apache Group/Apache2/cgi-bin">
    AllowOverride None
    Options ExecCGI
    Order allow,deny
    Allow from all
</Directory>
AddHandler cgi-script .cgi .pl
别的设置:
DirectoryIndex index.html index.html.var
DocumentRoot "E:/www" 
ScriptAlias /cgi-bin/ "E:/www/cgi-bin/"


E:/www是放网页首页的地方!!!
【2】把你写的 CGI程序XX.YYY放到D:/Apache Group/Apache2/cgi-bin下
//C语言
int main()
{
printf( "Content-type:text/html\n\n ");
printf( " <html> <title> haha </title> <body>  </br>\n ");

printf("Hello CGI </br>\n");

printf( " </body>  </html> ");
return  0;
}
//编译后生成 Try.exe


另外写个测试程序:
<html>
<title>你的第一个CGI测试程序 </title>
<body>
<h1>你的第一个CGI测试程序 <h1> <br>

<form  action="cgi-bin/try.exe"  method="post"> 
<input  type=submit value="查看CGI环境变量">
</form>

</body>
</html>
存到E:/www, 命名为index.html

在客户机的浏览器上, 打开http://XX.XX.XX.XX首页后,点击按钮【查看CGI环境变量】应该会看到 :
  Hello CGI 

End!
By DJStar

作者: deng0jun   发布时间: 2008-07-29

刚才有个小错误:

改为:
<Directory "E:/www/cgi-bin">
    AllowOverride None
    Options ExecCGI
    Order allow,deny
    Allow from all
</Directory>

作者: deng0jun   发布时间: 2008-07-29

cgic , cgihtml两个库都可以用来开发cgi。

作者: ernestzhang   发布时间: 2008-08-04

1、首先要有一个能解析CGI的web服务器
2、写好你的c程序
3、编译 gcc -o test.cgi test.c
4、拷贝程序都你的web服务器webroot目录,并赋予执行权限
5、按正确路径访问

作者: tyyhong   发布时间: 2008-08-21

引用 10 楼 tyyhong 的回复:
1、首先要有一个能解析CGI的web服务器
2、写好你的c程序
3、编译 gcc -o test.cgi test.c
4、拷贝程序都你的web服务器webroot目录,并赋予执行权限
5、按正确路径访问


恩。

楼主在具体实施中,注意编译器的使用、服务器搭建、访问方式。

作者: xiaoshun123   发布时间: 2010-04-30

1、首先要有一个能解析CGI的web服务器 
2、写好你的c程序 
3、编译 gcc -o test.cgi test.c 
4、拷贝程序都你的web服务器webroot目录,并赋予执行权限 
5、按正确路径访问


大体上是这样

作者: xindeluoye   发布时间: 2010-08-18