Linux下用gSOAP開發Web Service服務端和客戶端程序(二)

C++版本的程序:

(1)頭文件不變,還是SmsWBS.h

(2)Makefile文件:

GSOAP_ROOT=/usr/local/gSOAP
WSNAME0=soap
WSNAME=SmsWBS
CC=g++ -g -DWITH_NONAMESPACES
INCLUDE=-I $(GSOAP_ROOT)/include
SERVER_OBJS=$(WSNAME0)C.o $(WSNAME0)$(WSNAME)Service.o stdsoap2.o
CLIENT_OBJS=$(WSNAME0)$(WSNAME)Proxy.o $(WSNAME0)C.o stdsoap2.o
ALL_OBJS=${WSNAME}server.o $(WSNAME0)C.o $(WSNAME0)$(WSNAME)Service.o $(WSNAME0)$(WSNAME)Proxy.o ${WSNAME}test.o
#GSOAP_SRC=/usr/local/gsoap-2.7/gsoap

all:server

${WSNAME}.wsdl:${WSNAME}.h
        $(GSOAP_ROOT)/bin/soapcpp2 -i $(GSOAP_ROOT)/import ${WSNAME}.h

stdsoap2.o:$(GSOAP_ROOT)/src/stdsoap2.cpp
        $(CC) -c $? $(INCLUDE)

$(ALL_OBJS):%.o:%.cpp
        $(CC) -c $? $(INCLUDE)

server:Makefile ${WSNAME}.wsdl ${WSNAME}server.o $(SERVER_OBJS)
        $(CC) ${WSNAME}server.o $(SERVER_OBJS) -o ${WSNAME}server

client:Makefile ${WSNAME}.wsdl ${WSNAME}test.cpp $(ALL_OBJS) stdsoap2.o
        $(CC) ${WSNAME}test.o $(CLIENT_OBJS) -o ${WSNAME}test

clean:
        rm -f *.o *.xml *.a *.wsdl *.nsmap $(WSNAME0)H.h $(WSNAME0)C.cpp $(WSNAME0)Server.cpp  $(WSNAME0)Stub.* $(WSNAME0)$(WSNAME)S
ervice.* $(WSNAME0)$(WSNAME)Proxy.* $(WSNAME0)$(WSNAME)Object.* $(WSNAME0)ServerLib.cpp $(WSNAME0)ClientLib.cpp $(WSNAME)server ns.x
sd $(WSNAME)test

(3)服務端程序SmsWBSserver.cpp:

#include "soapSmsWBSService.h"
#include "SmsWBS.nsmap"

int main(int argc, char **argv)

 SmsWBSService sms;

 if (argc < 2)
  sms.serve();       /* serve as CGI application */
 else
 { 
  int port = atoi(argv[1]);

  if (!port)
  { 
   fprintf(stderr, "Usage: SmsWBSserver++ <port>\n");
   exit(0);
  }

  /* run iterative server on port until fatal error */
  if (sms.run(port))
  { 
   sms.soap_stream_fault(std::cerr);
   exit(-1);
  }
 }

 return 0;
}

int SmsWBSService::add(int num1, int num2, int *sum)
{
 *sum = num1 + num2;
 return SOAP_OK;
}

(4)客戶端程序SmsWBStest.cpp:

#include <stdio.h>
#include <stdlib.h>
#include "soapSmsWBSProxy.h"
#include "SmsWBS.nsmap"

int main(int argc, char **argv)
{
        int result = -1;
        char* server="http://localhost:9000";

        int num1 = 0;
        int num2 = 0;
        int sum = 0;

        if( argc < 3 )
        {
                printf("usage: %s num1 num2 \n", argv[0]);
                exit(0);
        }

        num1 = atoi(argv[1]);
        num2 = atoi(argv[2]);

        SmsWBSProxy  sms;
        result = sms.add(num1, num2, &sum);
        if (result != 0)
        {
                printf("soap err, errcode = %d \n", result);
        }
        else
        {
                printf("%d + %d = %d \n", num1, num2, sum);
        }

        return 0;
}

(5)編譯運行,與C版本類似,只是服務端運行時沒有提示信息

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章