gsoap服务端的开发及注意事项

来源:互联网 发布:临沂淘宝仓库招聘 编辑:程序博客网 时间:2024/06/05 02:23

         1. 可以先用JAVA生成接口的WSDL文件;

         2. 自己编写一个接口文件,然后使用" soapcpp2 -i soapsample.h" 生成C的服务端和客户端,其中服务端文件为soapsamleService.h. soapsamleService.cpp, 客户端为soapsampleProxy.h, soapsampleProxy.cpp文件;

         3. 编写服务端的监听程序, 实现接口函数;

 

         需要注意的几点:

           1  如果客户端为JAVA程序,则附件的格式应该为 BIMARY;

           2  接口函数应该始终返回0,否则客户端无法收到服务端返回的消息;

           3  使用soap_set_mode( soap, SOAP_C_UTFSTRING)来设置编码方式。

            具体开发说明请参看Gsoap的官方网站http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc7.2.4

         以下为gsoap的一个例子:

#include "soapH.h"
#include < pthread.h >
#define BACKLOG (100) // Max. request backlog
int main(int argc, char **argv)
{
   struct soap soap;
   soap_init(&soap);
   if (argc < 2) // no args: assume this is a CGI application
   {
      soap_serve(&soap); // serve request, one thread, CGI style
      soap_destroy(&soap); // dealloc C++ data
      soap_end(&soap); // dealloc data and clean up
   }
   else 
   {
      soap.send_timeout = 60; // 60 seconds
      soap.recv_timeout = 60; // 60 seconds
      soap.accept_timeout = 3600; // server stops after 1 hour of inactivity
      soap.max_keep_alive = 100; // max keep-alive sequence
      void *process_request(void*);
      struct soap *tsoap;
      pthread_t tid;
      int port = atoi(argv[1]); // first command-line arg is port
      SOAP_SOCKET m, s;
      m = soap_bind(&soap, NULL, port, BACKLOG);
      if (!soap_valid_socket(m))
         exit(1);
      fprintf(stderr, "Socket connection successful %d/n", m);
      for (;;)
      {
         s = soap_accept(&soap);
         if (!soap_valid_socket(s))
         {
            if (soap.errnum)
            {
               soap_print_fault(&soap, stderr);
               exit(1);
            }
            fprintf(stderr, "server timed out/n");
            break;
         }
         fprintf(stderr, "Thread %d accepts socket %d connection from IP %d.%d.%d.%d/n", i, s, (soap.ip >> 24)&0xFF, (soap.ip >> 16)&0xFF, (soap.ip >> 8)&0xFF, soap.ip&0xFF);
         tsoap = soap_copy(&soap); // make a safe copy
         if (!tsoap)
            break;
         pthread_create(&tid, NULL, (void*(*)(void*))process_request, (void*)tsoap);
      }
   }
   soap_done(&soap); // detach soap struct
   return 0;
}
void *process_request(void *soap)
{
   pthread_detach(pthread_self());
   soap_serve((struct soap*)soap);
   soap_destroy((struct soap*)soap); // dealloc C++ data
   soap_end((struct soap*)soap); // dealloc data and clean up
   soap_done((struct soap*)soap); // detach soap struct
   free(soap);
   return NULL;
}