嵌入式web服务器后台cgi使用socket与其他进程进行通信的源码

       目前项目中遇到了cgi与另一个进程进行通信的问题,我先试验了消息机制,发现消息机制根本行不通,不可靠,网上说的最多的cgi使用共享内存与其他进程通信。我这里使用socket通信,cgi作为客户端,另一个进程作为服务端。

这样很好解决了通信问题。这里的cgi后台程序使用了广泛用到的cgic库。

 

客户端源代码:

/*
  * socketClient.c
  *
  *  Created on: 2013年8月24日
  *      Author: lgf
  */
 
#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include "common.h"
 
int   socketClient(msg_t* pmsgClient, msg_t* pmsgServer);
 
int   socketClient(msg_t* pmsgClient, msg_t* pmsgServer)
{
     int   sock_fp;
     struct   sockaddr_in pin;
     struct   hostent *server_host_name;
     char   *msg= "ok,socket!" ;
     char   buff[2561];
 
     char   *host_name =  "192.168.0.121" ;
     int   port=7777;
 
     if (pmsgClient == NULL || pmsgServer==NULL)
     {
         x_debug( "pmsgClient == NULL || pmsgServer==NULL!!" );
         return   -1;
     }
 
     server_host_name = gethostbyname(host_name);
     if (server_host_name==0)
     {
         x_debug( "can not resolving localhost\n" );
         //x_debug("can not resolving localhost\n");
         return   -1;
     }
     bzero(&pin, sizeof (pin));
     pin.sin_family = AF_INET;
     pin.sin_addr.s_addr = htonl(INADDR_ANY);
     pin.sin_port = htons(port);
     sock_fp= socket(AF_INET, SOCK_STREAM, 0);
     if (sock_fp==-1)
     {
         x_debug( "cannot open socket " );
         //x_debug("cannot open socket ");
         return   -1;
 
     }
     if (connect(sock_fp, ( void   *)&pin,  sizeof (pin))==-1)
     {
         x_debug( "con not connecting to server\n" );
         //x_debug("con not connecting to server\n");
         close(sock_fp);
         return   -1;
     }
 
 
     if (send(sock_fp,pmsgClient, sizeof (msg_t),0)==-1)
     {
         x_debug( "can not send" );
         //x_debug("can not send");
         close(sock_fp);
         return   -1;
 
     }
 
     if (recv(sock_fp,pmsgServer, sizeof (msg_t),0)!=-1)
     {
 
     }
     else
     {
         x_debug( "can not recv" );
         close(sock_fp);
         return   -1;
     }
     x_debug( "socket client---------------- socket end." );
 
     close(sock_fp);
     return   0;
}

 

 

服务端源代码:

 

/*
  * socketServer.c
  *
  *  Created on: 2013年8月24日
  *      Author: lgf
  */
  
#include <stdio.h>
#include "cgic.h"
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/stat.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h>
#include "common.h"
  
  
//登入
void   msg_login(msg_t* pmsg);
  
  
int   socketServer();
  
int   main()
{
     socketServer();
     return   0;
}
  
int   socketServer()
{
     int   port=7777;
     struct   sockaddr_in  sin ;
     struct   sockaddr_in pin;
     int   sock_fp;
     int   temp_sock_fp=-1;
     int   iCnt=0;
     int   runing=1;
     int   size_of_addr;
     char   *msg= "hello internet!" ;
     char   buff[2561];
  
     msg_t msg_client;
     msg_t msg_server;
  
  
     sock_fp = socket(AF_INET,SOCK_STREAM,0);
     if (sock_fp==-1)
     {
         printf ( "socket fail.\n" );
         return   -1;
     }
  
     bzero(& sin , sizeof ( sin ));
     sin .sin_family=AF_INET;
     sin .sin_addr.s_addr=INADDR_ANY;
     sin .sin_port = htons(port);
  
     if (bind(sock_fp,( struct   sockaddr *)& sin , sizeof ( sin ))==-1)
     {
         printf ( "socket server bind fail.\n" );
         close(sock_fp);
         return   -1;
     }
     if (listen(sock_fp,20)==-1)
     {
         printf ( "socket server listen fail.\n" );
         close(sock_fp);
         return   -1;
     }
     printf ( "write to recive:\n" );
     while (runing)
     {
         iCnt++;
//      if(iCnt>15)
//      {
//          break;
//      }
         printf ( "iCnt=%d\n" ,iCnt);
         temp_sock_fp=accept(sock_fp,( struct   sockaddr *)&pin,&size_of_addr);
         if (temp_sock_fp==-1)
         {
             printf ( "accept!\n" );
             continue ;
         }
         printf ( "server wait message\n" );
         if (recv(temp_sock_fp,&msg_client, sizeof (msg_client),0)==-1)
         {
             printf ( "recv err\n" );
             close(temp_sock_fp);
             continue ;
         }
         switch (msg_client.msgId)
         {
             case   ITC_LOGIN: //验证登入用户名和密码
                 msg_login(&msg_client);
                 break ;
             default :
                 printf ( "\n---run to default---\n" );
                 break ;
         }
         printf ( "\n---send msg---\n" );
         //注意整体拷贝之后再赋消息类型值
         memcpy (( void   *)&msg_server,( void   *)&msg_client, sizeof (msg_t));
         if (send(temp_sock_fp,&msg_server, sizeof (msg_t),0)==-1)
         {
             printf ( "send err\n" );
         }
         close(temp_sock_fp);
     }
  
     printf ( "\n---out thread---\n" );
     sleep(2);
     close(sock_fp);
     return   0;
}

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值