gsoap 服务端和客户端实现简单的握手

来源:互联网 发布:八大山人 知乎 编辑:程序博客网 时间:2024/05/17 01:09

linux下用C++实现gsoap服务端和客户端实现简单通,本代码是在gsoap-2.8版本下进行。

client.cpp

#include<iostream>
#include "soapH.h"
//#include"soapStub.h"
#include"soapAAAServiceSoapBindingProxy.h"
#include "AAAServiceSoapBinding.nsmap"


using namespace std;


int main(int argc, char **argv) {
  /*      if ( argc != 2 ) {
                printf("Usage: %s stock_code/n", argv[0]);
                exit(-1);
        }   */
        char* server="http://localhost:8080";
        int result;
        struct soap client_soap;
        soap_init(&client_soap);
    
        string response;
        result=soap_call_ns1__authentication(&client_soap,server,NULL,"hello~~~",response);
        if(result ==SOAP_OK)
   cout<<response<<endl;
        
        else
            soap_print_fault(&client_soap,stderr);
            
        soap_destroy(&client_soap);
        soap_end(&client_soap);
        soap_done(&client_soap);
        return 0;
}

server.cpp

#include"soapH.h"
#include"AAAServiceSoapBinding.nsmap"
#include "soapAAAServiceSoapBindingObject.h"


using namespace std;


int ns1__authentication(struct soap*, std::string _in0, std::string &_authenticationReturn);
    
int main(int argc,char **argv)
{
    int m,s;
    struct soap add_soap;
    soap_init(&add_soap);
    soap_set_namespaces(&add_soap,namespaces);
    
    if(argc<2){
        printf("usage:%s <server_port> /n",argv[0]);
        exit(1);
    }else{
        m=soap_bind(&add_soap,NULL,atoi(argv[1]),100);
        if(m<0){
            soap_print_fault(&add_soap,stderr);
            exit(-1);
        }
        printf("Socket connection successful:master socket = %d/n",m);
        for(;;){
            s=soap_accept(&add_soap);
            if(s<0){
                soap_print_fault(&add_soap,stderr);
                exit(-1);
            }
            
            printf("Socket connection successful:slave socket=%d/n",s);
            soap_serve(&add_soap);
            soap_end(&add_soap);
        }
    }
    return 0;
}


int ns1__authentication(struct soap*, std::string _in0, std::string &_authenticationReturn)
{
    cout<<_in0<<endl;
    _authenticationReturn="succeed!";
    return 0;
}

原创粉丝点击