gsoap编译及使用例子

http://sourceforge.net/projects/gsoap2/
下载gsoap源码
解压:
$ unzip gsoap_2.8.17.zip
编译:
$ cd gsoap-2.8/
$ configure
$ make
$ sudo make install

add.h文件


  1. //gsoap ns service name: add
  2. //gsoap ns service style: rpc
  3. //gsoap ns service encoding: encoded
  4. //gsoap ns service namespace: http://localhost/add.wsdl
  5. //gsoap ns service location: http://localhost/add.cgi
  6. //gsoap ns schema namespace: urn:add

  7. typedef std::string xsd__string;
  8. struct abc {
  9.     int a;
  10.     double b;
  11.     char *c;
  12. };

  13. int ns4__add(int num1, int num2, int* sum);
  14. int ns4__str(xsd__string src, xsd__string *result);
  15. int ns4__abcstruct(struct abc *result);
  16. int ns4__Add100(int InValue, int &result);
addClient.cpp文件


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <string>
  5. #include "soapStub.h"
  6. #include "ns4.nsmap"
  7. #include "soapProxy.h"


  8. using namespace std;


  9. int main(int argc, char **argv)
  10. {
  11.     int result = -1;
  12.     char server[128] = {0};
  13.     if (argc < 2) {
  14.         printf("usage: %s <ip:port>\n", argv[0]);
  15.         exit(1);
  16.     }
  17.     strcpy(server,argv[1]);


  18.     Proxy add;
  19.     add.soap_endpoint = server;
  20.     int sum = 0;
  21.     result = add.Add100(10, sum);
  22.     printf("Add100(10)=%d\n", sum);
  23.     result = add.add(3, 4, &sum);
  24.     if (result != 0) {
  25.         printf("soap error, errcode=%d\n", result);
  26.     } else {
  27.         printf("%d + %d = %d\n", 3, 4, sum);
  28.     }


  29.     string str;
  30.     result = add.str(string("world"), &str);
  31.     if (result != 0) {
  32.         printf("soap error, errcode=%d\n", result);
  33.     } else {
  34.         cout << str << endl;
  35.     }


  36.     struct abc st;
  37.     result = add.abcstruct(&st);
  38.     printf("abcstruct:%d %lf %s\n", st.a, st.b, st.c);


  39.     return 0;
  40. }

addServer.cpp文件


  1. #include <string>
  2. #include "soapH.h"
  3. #include "soapService.h"
  4. #include "ns4.nsmap"


  5. using namespace std;


  6. int Service::add(int num1, int num2, int *sum)
  7. {
  8.     *sum = num1 + num2;
  9.     return SOAP_OK;
  10. }


  11. int Service::str(string src, string *result)
  12. {
  13.     *result = string("hello ") + src;
  14.     cout << *result << endl;
  15.     return 0;
  16. }


  17. int Service::abcstruct(struct abc *result)
  18. {
  19.     result->= 1;
  20.     result->= 2.0;
  21.     result->c= new char[2];
  22.     result->c[0] = 'a';
  23.     result->c[1] = '\0';
  24.     return 0;
  25. }


  26. int Service::Add100(int num, int &result)
  27. {
  28.     result = num + 100;
  29.     return 0;
  30. }


  31. int main(int argc, char **argv)
  32. {
  33.     int m, s;
  34.     Service add;
  35.     if (argc < 2)
  36.     {
  37.         printf("usage: %s <server_port> /n", argv[0]);
  38.         return -1;
  39.     }


  40.     if (add.run(atoi(argv[1])))
  41.     {
  42.         printf( "add service run failed\n" );
  43.         return -1;
  44.     }


  45.     return 0;
  46. }
Makefile文件


  1. #GSOAP_ROOT = /root/gsoap-2.7/gsoap
  2. WSNAME = add
  3. CC = g++ --DWITH_NONAMESPACES
  4. #INCLUDE = -I$(GSOAP_ROOT)
  5. SERVER_OBJS = soapC.o soapService.o $(WSNAME)Server.
  6. CLIENT_OBJS = soapC.o soapProxy.o $(WSNAME)Client.o
  7. all: server client
  8. server: $(SERVER_OBJS) 
  9.     $(CC) $(INCLUDE) -o $(WSNAME)Server $(SERVER_OBJS) -lgsoapssl++ -lssl -lcrypto -lz
  10. client: $(CLIENT_OBJS)
  11.     $(CC) $(INCLUDE) -o $(WSNAME)Client $(CLIENT_OBJS) -lgsoapssl++ -lssl -lcrypto -lz 
  12. clean:
  13.     rm -*.*.xml *.*.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd

编译:
soapcpp2 -i add.h
make

运行:
服务器
./addServer 9981
客户端
./addClient http://localhost:9981
以下是打印
Add100(10)=110
3 + 4 = 7
hello world
abcstruct:1 2.000000 a

其他一些总结:
如果自己写example程序,发现编译不过:
soapProxy.o:在函数‘Proxy::~Proxy()’中:
soapProxy.cpp:(.text+0x52):对‘soap::~soap()’未定义的引用
soapProxy.o:在函数‘Proxy::reset()’中:
soapProxy.cpp:(.text+0xec):对‘soap_initialize’未定义的引用
soapProxy.o:在函数‘Proxy::testChinese(char const*, char const*, char**)’中:
soapProxy.cpp:(.text+0x27a):对‘soap_url’未定义的引用
soapProxy.o:在函数‘Proxy::Proxy()’中:
soapProxy.cpp:(.text+0x40d):对‘soap::soap()’未定义的引用
soapProxy.cpp:(.text+0x44b):对‘soap::~soap()’未定义的引用
soapProxy.o:在函数‘Proxy::reset()’中:
soapProxy.cpp:(.text+0xec):对‘soap_initialize’未定义的引用
soapProxy.o:在函数‘Proxy::testChinese(char const*, char const*, char**)’中:
soapProxy.cpp:(.text+0x27a):对‘soap_url’未定义的引用
collect2: error: ld returned 1 exit status

解决方法:
Makefile不用-lgsoap或者-lgsoapssl++方式链接gsop
而是直接把std2Soap.cpp 加入到工程中一起与自己的程序编译

gsoap下的samples示例工程
oneway  与心跳包有关
httpcookies  cookies有关

要支持SSl,GZIP,编译命令中加 -DWITH_OPENSSL -DWITH_GZIP -lssl -lcrypto -lz

中文乱码解决:
struct soap soap;
soap_init(&soap);
soap_set_mode(&soap, SOAP_C_UTFSTRING); // 程序中加这个

设置连接超时(秒为单位):
soap->connect_timeout=3;

设置收发超时:
soap->send_timeout = 30;
soap->recv_timeout = 30;
作者:帅得不出门 程序员群:31843264
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值