CGCI GET 通讯、进程间共享内存通讯

CGCI GET 通讯、进程间共享内存通讯:
1.前后端代码:


   用于get的测试

function get_test()
{

var senddata =   "GET_000";
$.ajax({
    "type": "get",
    "url": "cgi-bin/index.cgi", // 此处的cgi-bin 实际映射文件路径为/cgi-html/cgi-bin/ 

//boa 配置的cgi路径为 ScriptAlias /cgi-bin/ /cgi-html/cgi-bin/
“cache”: true,
“data” : senddata,
“dataType”: “html”
}).done(function(data, status, xhr) {

    if (status != 'success')
    {
        alert("fail");
    } else {
        alert("receive data is " + data);
    }
})

}

  1. CGCI 嵌入式代码:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>  
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <syslog.h>
#include    <sys/types.h>  
#include    <sys/ipc.h>  
#include    <sys/shm.h>  
#include    <stdio.h>  
#include    <error.h>  

char * method;
char * p ;
int len;
char *post_buff;

/// 消息队列相关
unsigned char  msg_send_buff[512];
int msg_snd_fd=0;
int msg_snd_ret=0;
#define msg_boa_passwd  1000 

/  共享内存/
#define ShareMemSize  1024
#define ShareMemMode  (SHM_R | SHM_W| IPC_CREAT)  
#define ShareMemId    100 
static int     * ShareMemPtr ;
static int     ShareMemFd;
static int     FromShareMemGetDateBuffer[1024] ;

static int  GetDataFromShareMem ()
{

   FromShareMemGetDateBuffer[0] = ShareMemPtr[0];  // 另一进程将变量写到ShareMemPtr 所指向的内存控件 ; 从共享内存中获取数据
   FromShareMemGetDateBuffer[1] = ShareMemPtr[1];
   FromShareMemGetDateBuffer[2] = ShareMemPtr[2];
   FromShareMemGetDateBuffer[3] = ShareMemPtr[3];
   FromShareMemGetDateBuffer[4] = ShareMemPtr[4];

   return 1 ;
}
int main(void)
{  
   // 创建共享内存用于跟网页cgi 交互数据
   ShareMemFd = shmget(ShareMemId, ShareMemSize, ShareMemMode );
   if (ShareMemFd < 0)
   {
    syslog(LOG_INFO,"share mem create fail\n" ); 
   }
   ShareMemPtr = shmat(ShareMemFd, 0, 0) ;// 必须创建共享内存映射
   if (ShareMemPtr ==(void *) -1)
   {
     syslog(LOG_INFO,"share mem shmat function call fail\n" );
   }

   method = getenv("REQUEST_METHOD"); // val 是 POST  或者GET
   if(method==0)
   {
     printf("can't receive data from net ");
   }
   else if( strcmp(method,"POST")==0 )  //post 方法
           {    
             p = getenv("CONTENT_LENGTH");  //获取POST的环境变量 
             if(p)
             {
                     len =atoi(p); //把字符串转换成整型数
               post_buff  = malloc(sizeof(char)*len + 1); //必须申请缓存,因为stdin是不带缓存的
               fread(post_buff,sizeof(char),len,stdin);//将POST的值读到post_buff
               post_buff[len] ='\0' ;


              / 收到的数据返回到网页中用于验证

               printf("Content-type:text/html\n\n"); //  必不可小,否则ajax返回数据后,html文件不解析            
               printf("%s",post_buff);
               memcpy(msg_send_buff,post_buff,len);
               msg_snd_fd=msgget(msg_boa_passwd,0666|IPC_CREAT);
               if(msg_snd_fd<0)
               {      
                  syslog(LOG_INFO,"msgget is fail \n");
               }
               //放入消息队列      
               msg_snd_ret=msgsnd(msg_snd_fd,(void *)(&msg_send_buff[0]),len,IPC_NOWAIT);// 当队列满时返回错误标志
               if(msg_snd_ret==0)
               {
               syslog(LOG_INFO,"msgsnd is suncessful \n");
               }
               else
               {
                  syslog(LOG_INFO,"msgsnd is FAIL.return val is %d \n",msg_snd_ret);
               }

             }
             else
             {
                len =0;
                       printf("len is null");
             }
               return 1 ;   
           }
   else if ( strcmp(method,"GET")==0) // GET 方法
           {
             p = getenv("QUERY_STRING");     //从环境变量 // get 方式收到的数据保存在QUERY_STRING的环境变量中             
             if( p!=NULL)
             {  
            // app 与此进程是通过共享方式获得数据
            // 在app进程中每1段时间将需要的变量写入到共享内存中

                     //  从共享内存中获取数据
                     GetDataFromShareMem() ;
                         if(strncmp(p,"GET_000",7)==0)  //  数据已经保存到 p指针中
    {
        printf("Content-type:text/html\n\n"); // 必不可少
        printf("%d",FromShareMemGetDateBuffer[0]);
    }
    else if(strncmp(p,"GET_001",7)==0)
    {
        printf("Content-type:text/html\n\n");
        printf("%d",FromShareMemGetDateBuffer[1]);
    }
     else if(strncmp(p,"GET_002",7)==0)
    {
        printf("Content-type:text/html\n\n");
        printf("%d",FromShareMemGetDateBuffer[2]);
    }
     else if(strncmp(p,"GET_003",7)==0)
    {
        printf("Content-type:text/html\n\n");
        printf("%d",FromShareMemGetDateBuffer[3]);
    }
     else if(strncmp(p,"GET_004",7)==0)
    {
        printf("Content-type:text/html\n\n");
        printf("%d",FromShareMemGetDateBuffer[4]);
    }
     else if(strncmp(p,"GET_005",7)==0)
    {
        printf("Content-type:text/html\n\n");
        printf("%d",FromShareMemGetDateBuffer[5]);
    }
     else if(strncmp(p,"GET_001",7)==0)
    {
        printf("Content-type:text/html\n\n");
        printf("%d",FromShareMemGetDateBuffer[6]);
    }

             }

             return 2;
           }
}

3.  共享内存写入的那段代码:

#define ShareMemSize  1024
#define ShareMemMode  (SHM_R | SHM_W| IPC_CREAT)  
#define ShareMemId    100 
int     * ShareMemPtr ;
int     ShareMemFd;
int     g_system_uptime  =0 ;  // 每10s 加1 

  ShareMemFd = shmget(ShareMemId, ShareMemSize, ShareMemMode );
   if (ShareMemFd < 0)
   {
    syslog(LOG_INFO,"share mem create fail\n" ); 
   }
   ShareMemPtr = shmat(ShareMemFd, 0, 0) ;// 共享内存映射
   if (ShareMemPtr ==(void *) -1)
   {
     syslog(LOG_INFO,"share mem shmat function call fail\n" );
   }

  if((timer_count % 50) == 0) //10s
   {
     ShareMemPtr[0] = gt_Lxl_Operation.dev_version.hardware_version  ; //  对共享内存的数据进行赋值
     ShareMemPtr[1] = gt_Lxl_Operation.dev_version.software_version; 
     ShareMemPtr[2] =  0;// 设备名称
     //ShareMemPtr[3] =  ;// 设备名称
     g_system_uptime ++ ;
     ShareMemPtr[4] = g_system_uptime ;// 上线时间 g_system_uptime
   }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值