gSOAP简单多线程服务器程序

gSOAP简单多线程服务器程序 
 
 
一 gSOAP需要的头文件:
[cpp]  view plain  copy
  1. //gsoap ns service name: calc  
  2. //gsoap ns service style: rpc  
  3. //gsoap ns service encoding: encoded  
  4. //gsoap ns service namespace: http://127.0.0.1:8089/calc.wsdl  
  5. //gsoap ns service location: http://127.0.0.1:8089/cal  
  6. //gsoap ns schema  namespace:    urn:calc  
  7. int ns__add(double a, double b, double *result);  
  8. int ns__sub(double a, double b, double *result);  
  9. int ns__mul(double a, double b, double *result);  
  10. int ns__div(double a, double b, double *result);  
  11. int ns__pow(double a, double b, double *result);  

二 多线程服务器关键代码

[cpp]  view plain  copy
  1. #include    
  2. #include  "calc.nsmap"  
  3. #include  "soapH.h"  
  4.   
  5. /  
  6. ///宏与全局变量的定义  
  7. #define  BACKLOG (100)    
  8. #define  MAX_THR (10)     
  9. #define  MAX_QUEUE (1000)  
  10.   
  11.   
  12. pthread_mutex_t queue_cs;                        //队列锁  
  13. pthread_cond_t  queue_cv;                          //条件变量  
  14. SOAP_SOCKET     queue[MAX_QUEUE];   //数组队列  
  15. int                           head =0, tail =0;          //队列头队列尾初始化           
  16. //  
  17.   
  18.   
  19. //  
  20. void *      process_queue(void *);        //线程入口函数  
  21. int         enqueue(SOAP_SOCKET);  //入队列函数  
  22. SOAP_SOCKET dequeue(void);         //出队列函数  
  23.   
  24. //  
  25. //线程入口函数  
  26. void * process_queue(void * soap)  
  27. {  
  28.   struct soap * tsoap = (struct soap *)soap;  
  29.   for(;;)  
  30.   {  
  31.         tsoap->socket = dequeue();  
  32.         if (!soap_valid_socket(tsoap->socket))  
  33.        {  
  34.          break;  
  35.         }  
  36.         soap_serve(tsoap);  
  37.         soap_destroy(tsoap);  
  38.         soap_end(tsoap);  
  39.   }  
  40.   return NULL;  
  41. }  
  42.   
  43. //入队列操作  
  44. int enqueue(SOAP_SOCKET sock)  
  45. {  
  46.   int status = SOAP_OK;  
  47.   int next;  
  48.   pthread_mutex_lock(&queue_cs);  
  49.   next = tail +1;  
  50.   if (next >= MAX_QUEUE)   
  51.     next = 0;  
  52.   if (next == head)   
  53.       status = SOAP_EOM;  
  54.   else  
  55.   {  
  56.     queue[tail] =sock;  
  57.     tail = next;  
  58.   }  
  59.   pthread_cond_signal(&queue_cv);  
  60.   pthread_mutex_unlock(&queue_cs);  
  61.   return status;  
  62. }  
  63.   
  64. //出队列操作  
  65. SOAP_SOCKET dequeue()  
  66. {  
  67.   SOAP_SOCKET sock;  
  68.   pthread_mutex_lock(&queue_cs);  
  69.    while (head == tail )  
  70.    {  
  71.           pthread_cond_wait(&queue_cv,&queue_cs);  
  72.    }  
  73.   sock = queue[head++];  
  74.   if (head >= MAX_QUEUE)  
  75.         {  
  76.     head =0;  
  77.   }  
  78.   pthread_mutex_unlock(&queue_cs);  
  79.   return sock;  
  80. }  
  81.   
  82.   
  83. //具体服务方法  
  84. //加法的实现  
  85. int ns__add(struct soap *soap, double a, double b, double *result)  
  86. {  
  87.       *result = a + b;  
  88.       return SOAP_OK;  
  89. }   
  90. //减法的实现  
  91. int ns__sub(struct soap *soap, double a, double b, double *result)  
  92. {   
  93.      *result = a - b;  
  94.      return SOAP_OK;  
  95. }   
  96. //乘法的实现  
  97. int ns__mul(struct soap *soap, double a, double b, double *result)  
  98. {   
  99.      *result = a * b;  
  100.      return SOAP_OK;  
  101. }   
  102. //除法的实现  
  103. int ns__div(struct soap *soap, double a, double b, double *result)  
  104. {   
  105.    if (b)  
  106.        *result = a / b;  
  107.    else  
  108.   {  
  109.          char *s = (char*)soap_malloc(soap, 1024);  
  110.          sprintf(s, "Can't">http://tempuri.org/">Can't divide %f by %f", a, b);  
  111.          return soap_sender_fault(soap, "Division by zero", s);  
  112.   }  
  113.   return SOAP_OK;  
  114. }   
  115. //乘方的实现  
  116. int ns__pow(struct soap *soap, double a, double b, double *result)  
  117. {   
  118.   *result = pow(a, b);  
  119.   if (soap_errno == EDOM) /* soap_errno 和errorno类似, 但是和widnows兼容 */  
  120.   {   
  121.     char *s = (char*)soap_malloc(soap, 1024);  
  122.     sprintf(s, "Can't take the power of %f to  %f", a, b);  
  123.     sprintf(s, "Can't">http://tempuri.org/">Can't take power of %f to %f", a, b);  
  124.     return soap_sender_fault(soap, "Power function domain error", s);  
  125.   }  
  126.   return SOAP_OK;  
  127. }  
  128.   
  129. //  
  130. //主函数  
  131. int main(int argc,char ** argv)  
  132. {  
  133.   struct soap ServerSoap;  
  134.      //初始话运行时环境  
  135.     soap_init(&ServerSoap);  
  136.     //如果没有参数,当作CGI程序处理  
  137.     if (argc <2)   
  138.     {         
  139.            //CGI 风格服务请求,单线程  
  140.           soap_serve(&ServerSoap);  
  141.           //清除序列化的类的实例  
  142.          soap_destroy(&ServerSoap);  
  143.          //清除序列化的数据  
  144.         soap_end(&ServerSoap);       
  145.    }else  
  146.    {  
  147.      struct soap * soap_thr[MAX_THR];  
  148.      pthread_t tid[MAX_THR];  
  149.      int i,port = atoi(argv[1]);  
  150.      SOAP_SOCKET m,s;  
  151.       //锁和条件变量初始化  
  152.      pthread_mutex_init(&queue_cs,NULL);  
  153.      pthread_cond_init(&queue_cv,NULL);  
  154.      //绑定服务端口  
  155.     m = soap_bind(&ServerSoap,NULL,port,BACKLOG);  
  156.     //循环直至服务套接字合法  
  157.     while (!soap_valid_socket(m))  
  158.    {  
  159.                 fprintf(stderr,"Bind port error! ");  
  160.                 m = soap_bind(&ServerSoap,NULL,port,BACKLOG);  
  161.     }  
  162.     fprintf(stderr,"socket connection successful %d ",m);  
  163.                   
  164.      //生成服务线程  
  165.     for(i = 0; i <MAX_THR; i++)  
  166.   
  167.    {  
  168.       soap_thr[i] = soap_copy(&ServerSoap);  
  169.       fprintf(stderr,"Starting thread %d ",i);  
  170.       pthread_create(&tid[i],NULL,(void*(*)(void*))process_queue,(void*)soap_thr[i]);  
  171.     }  
  172.                   
  173.     for(;;)  
  174.     {  
  175.       //接受客户端的连接  
  176.       s = soap_accept(&ServerSoap);  
  177.       if (!soap_valid_socket(s))   
  178.       {  
  179.         if (ServerSoap.errnum)   
  180.                                 {  
  181.           soap_print_fault(&ServerSoap,stderr);  
  182.           continue;  
  183.         }else  
  184.         {  
  185.           fprintf(stderr,"Server timed out ");  
  186.           break;  
  187.         }  
  188.       }  
  189.        //客户端的IP地址  
  190.       fprintf(stderr,"Accepted connection from IP= %d.%d.%d.%d socket = %d ",  
  191.                                ((ServerSoap.ip)>>24)&&0xFF,((ServerSoap.ip)>>16)&0xFF,((ServerSoap.ip)>>8)&0xFF,(ServerSoap.ip)&0xFF,(ServerSoap.socket));  
  192.       //请求的套接字进入队列,如果队列已满则循环等待  
  193.        while(enqueue(s) == SOAP_EOM)  
  194.                 Sleep(1000);  
  195.     }  
  196.     //服务结束后的清理工作  
  197.     for(i = 0; i < MAX_THR; i++)  
  198.     {  
  199.       while (enqueue(SOAP_INVALID_SOCKET) == SOAP_EOM)   
  200.        {  
  201.            Sleep(1000);  
  202.       }  
  203.     }  
  204.     for(i=0; i< MAX_THR; i++)  
  205.     {  
  206.       fprintf(stderr,"Waiting for thread %d to terminate ..",i);  
  207.       pthread_join(tid[i],NULL);  
  208.       fprintf(stderr,"terminated ");  
  209.       soap_done(soap_thr[i]);  
  210.       free(soap_thr[i]);  
  211.     }  
  212.     pthread_mutex_destroy(&queue_cs);  
  213.     pthread_cond_destroy(&queue_cv);  
  214.   }  
  215.     //分离运行时的环境  
  216.   soap_done(&ServerSoap);  
  217.   return 0;  
  218. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值