http://sourceforge.net/projects/gsoap2/
下载gsoap源码
解压:
$ unzip gsoap_2.8.17.zip
编译:
$ cd gsoap-2.8/
$ configure
$ make
$ sudo make install
add.h文件
-
//gsoap ns service name: add
-
//gsoap ns service style: rpc
-
//gsoap ns service encoding: encoded
-
//gsoap ns service namespace: http://localhost/add.wsdl
-
//gsoap ns service location: http://localhost/add.cgi
-
//gsoap ns schema namespace: urn:add
-
-
typedef std::string xsd__string;
-
struct abc {
-
int a;
-
double b;
-
char *c;
-
};
-
-
int ns4__add(int num1, int num2, int* sum);
-
int ns4__str(xsd__string src, xsd__string *result);
-
int ns4__abcstruct(struct abc *result);
-
int ns4__Add100(int InValue, int &result);
addClient.cpp文件
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
#include <string>
-
#include "soapStub.h"
-
#include "ns4.nsmap"
-
#include "soapProxy.h"
-
-
-
using namespace std;
-
-
-
int main(int argc, char **argv)
-
{
-
int result = -1;
-
char server[128] = {0};
-
if (argc < 2) {
-
printf("usage: %s <ip:port>\n", argv[0]);
-
exit(1);
-
}
-
strcpy(server,argv[1]);
-
-
-
Proxy add;
-
add.soap_endpoint = server;
-
int sum = 0;
-
result = add.Add100(10, sum);
-
printf("Add100(10)=%d\n", sum);
-
result = add.add(3, 4, &sum);
-
if (result != 0) {
-
printf("soap error, errcode=%d\n", result);
-
} else {
-
printf("%d + %d = %d\n", 3, 4, sum);
-
}
-
-
-
string str;
-
result = add.str(string("world"), &str);
-
if (result != 0) {
-
printf("soap error, errcode=%d\n", result);
-
} else {
-
cout << str << endl;
-
}
-
-
-
struct abc st;
-
result = add.abcstruct(&st);
-
printf("abcstruct:%d %lf %s\n", st.a, st.b, st.c);
-
-
-
return 0;
-
}
addServer.cpp文件
-
#include <string>
-
#include "soapH.h"
-
#include "soapService.h"
-
#include "ns4.nsmap"
-
-
-
using namespace std;
-
-
-
int Service::add(int num1, int num2, int *sum)
-
{
-
*sum = num1 + num2;
-
return SOAP_OK;
-
}
-
-
-
int Service::str(string src, string *result)
-
{
-
*result = string("hello ") + src;
-
cout << *result << endl;
-
return 0;
-
}
-
-
-
int Service::abcstruct(struct abc *result)
-
{
-
result->a = 1;
-
result->b = 2.0;
-
result->c= new char[2];
-
result->c[0] = ‘a‘;
-
result->c[1] = ‘\0‘;
-
return 0;
-
}
-
-
-
int Service::Add100(int num, int &result)
-
{
-
result = num + 100;
-
return 0;
-
}
-
-
-
int main(int argc, char **argv)
-
{
-
int m, s;
-
Service add;
-
if (argc < 2)
-
{
-
printf("usage: %s <server_port> /n", argv[0]);
-
return -1;
-
}
-
-
-
if (add.run(atoi(argv[1])))
-
{
-
printf( "add service run failed\n" );
-
return -1;
-
}
-
-
-
return 0;
-
}
Makefile文件
-
#GSOAP_ROOT = /root/gsoap-2.7/gsoap
-
WSNAME = add
-
CC = g++ -g -DWITH_NONAMESPACES
-
#INCLUDE = -I$(GSOAP_ROOT)
-
SERVER_OBJS = soapC.o soapService.o $(WSNAME)Server.o
-
CLIENT_OBJS = soapC.o soapProxy.o $(WSNAME)Client.o
-
all: server client
-
server: $(SERVER_OBJS)
-
$(CC) $(INCLUDE) -o $(WSNAME)Server
$(SERVER_OBJS) -lgsoapssl++ -lssl -lcrypto -lz
-
client: $(CLIENT_OBJS)
-
$(CC) $(INCLUDE) -o $(WSNAME)Client
$(CLIENT_OBJS) -lgsoapssl++ -lssl -lcrypto -lz
-
clean:
-
rm -f *.o *.xml *.a *.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;
作者:帅得不出门 程序员群:31843264gsoap编译及使用例子
原文:http://blog.csdn.net/zmlovelx/article/details/43021323