macos Cpp webserver的例子

一 、hello.h

#include <iostream>
using namespace std;

int ns__hello(std::string* name,std::string &greeting);
~                                                       

二、helloclient.cpp

#include "soapH.h"
#include "ns.nsmap"
#include <stdio.h>
#include <stdlib.h>
/*
SOAP_FMAC5 int SOAP_FMAC6 soap_call_ns__hello(struct soap *soap, const char *soap_endpoint, const char *soap_action, std::string *name, std::string &greeting)
{       if (soap_send_ns__hello(soap, soap_endpoint, soap_action, name) || soap_recv_ns__hello(soap, greeting))
                return soap->error;
        return SOAP_OK;
}


*/

int hello(struct soap *hello_soap,const char* server,  std::string *name, std::string &greeting)
{
   int result=0;
//    soap_set_namespaces(&hello_soap, hello_namespaces);
    //该函数是客户端调用的主要函数,后面几个参数和hello.h中声明的一样,前面多了3个参数,函数名是接口函数名ns__hello前面加上soap_call_
    soap_call_ns__hello( hello_soap, server, "", name,greeting);
    if(hello_soap->error)
    {
        printf("soap error:%d,%s,%s\n", hello_soap->error, *soap_faultcode(hello_soap), *soap_faultstring(hello_soap) );
        result = hello_soap->error;
    }
    else  std::cout<<"hello:"<<greeting <<std::endl;
    return result;
}



int main(int argc, char **argv)
{
    struct soap *hello_soap = soap_new();
    //soap_init(&hello_soap);
    int result = -1;
    // 后续使用完成后需要释放内存
    char* server=soap_strdup(hello_soap,"http://localhost:9100");
    std::string  name= "luoxuehua "; 
    std::string  greeting;
    //result = hello(&hello_soap,server,&name,greeting);
    result = hello(hello_soap,server,&name,greeting);

    if (result != 0)
    {
        printf("soap err,errcode = %d\n", result);
    }
    else
   {
         std::cout<<"main:result: "<< greeting <<std::endl;
   }
    //soap_destroy(&hello_soap);
    //soap_end(&hello_soap);
    //soap_done(&hello_soap);
    soap_destroy(hello_soap);
    soap_end(hello_soap);
    soap_free(hello_soap);
    //free(server);
    return 0;
}

三、helloserver.cpp

#include "hello.h"
#include "ns.nsmap"
#include <iostream>
using namespace std;

int main(int argc, char* argv[])
{
 
SOAP_SOCKET m, s; /* sockets */
  struct soap soap;
  soap_init(&soap);
  if (argc < 2)
    soap_serve(&soap);  /* serve as CGI application */
  else
  {
    m = soap_bind(&soap, NULL, atoi(argv[1]), 1);
    if (!soap_valid_socket(m))
    {
      soap_print_fault(&soap, stderr);
      exit(1);
    }
    fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
    for ( ; ; )
    {
      s = soap_accept(&soap);
      fprintf(stderr, "Socket connection successful: socket = %d\n", s);
      if (!soap_valid_socket(s))
      {
        soap_print_fault(&soap, stderr);
        exit(1);
      }
      soap_serve(&soap);
      soap_end(&soap);
    }
  }
  return 0;




}
//server端的实现函数与hello.h中声明的函数相同,但是多了一个当前的soap连接的参数
int  ns__hello(struct soap *soap,  std::string *name ,std::string &greeting)

{

    greeting ="Hello Webservice:";
   
    greeting.append(*name);
  return SOAP_OK;


}

四、编译

c++ -o  helloserver  helloserver.cpp soapC.cpp stdsoap2.cpp soapServer.cpp

c++ -o  helloclient helloclient.cpp soapC.cpp stdsoap2.cpp soapClient.cpp

 

posted @ 2024-08-01 18:51  atom_lxh  阅读(6)  评论(0编辑  收藏  举报