Ubuntu 下的webservices
搞 了一下午:
开发server程序。需使用gSOAP生成server端代码框架。
我们有两种做法:
- 编写WSDL,使用wsdl2h生成头文件,再soapcpp2生成框架代码。
- 编写头文件。使用soapcpp2生成框架代码;
这两种方式。结果是一样的。终于都有产生头文件。并生成代码。不同在于。在项目的开发中须要维护的文件不同。前者是须要维护WSDL文件,后者维护头文件。
我个人认为另外一种方式更好用,不不过少了个步骤。而是WSDL的语法太难写了,有点XSD的味道。而头文件的编写。更接近于程序猿的思考方式,比方定义消息结构,定义接口名称等。
gSOAP是很智能的,它利用C/C++的凝视来获取信息,所以在手工编写的头文件里,凝视是用用处的。常以// gsoap 名字空间 …开头。
做为学习。我准备为php blog程序wordpress写一个web service接口。名字叫wpsoap。
给出代码:
1
root@ubuntu:/home/aries/Aries/gsoap# cat add.h
//gsoapopt cw
//gsoap ns2 schema namespace: urn:add
//gsoap ns2 schema form: unqualified
//gsoap ns2 service name: add
//gsoap ns2 service type: addPortType
//gsoap ns2 service port:http://websrv.cs.fsu.edu/~engelen/addserver.cgi
//gsoap ns2 service namespace: urn:add
//gsoap ns2 service transport: http://schemas.xmlsoap.org/soap/http
//gsoap ns2 service method-style: add rpc
//gsoap ns2 service method-encoding:
//add http://schemas.xmlsoap.org/soap/encoding/
//gsoap ns2 service method-action: add ""
int ns2__add( int num1, int num2, int* sum );
注意:凝视的的内容也必须加上
2 运行soapcpp2 -c add.h
3 加入一个服务端 addserver.c
root@ubuntu:/home/aries/Aries/gsoap# cat addserver.c
#include "soapH.h"
#include "add.nsmap" 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);
}
fprintf(stderr, "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);
}
fprintf(stderr, "Socket connection successful: slave socket = %d/n", s);
soap_serve(&add_soap);
soap_end(&add_soap);
}
}
return 0;
} int ns2__add(struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return 0;
}
4 t加入 客服端 addclient.c
root@ubuntu:/home/aries/Aries/gsoap# cat addclient.c
#include "soapStub.h"
#include "add.nsmap" int add(const char *server, int num1, int num2, int *sum)
{
struct soap add_soap;
int result = 0;
soap_init(&add_soap);
soap_set_namespaces(&add_soap, namespaces);
soap_call_ns2__add(&add_soap, server, NULL, num1, num2, sum);
printf("server is %s, num1 is %d, num2 is %d/n", server, num1, num2); if (add_soap.error) {
printf("soap error: %d, %s, %s/n", add_soap.error, *soap_faultcode(&add_soap), *soap_faultstring(&add_soap));
result = add_soap.error;
}
soap_end(&add_soap);
soap_done(&add_soap);
return result;
}
5 測试 上面的addtest.c
root@ubuntu:/home/aries/Aries/gsoap# cat addtest.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h> int add(const char *server, int num1, int num2, int *sum);
int main(int argc, char **argv)
{
int result = -1;
char server[128] = {0};
int num1;
int num2;
int sum; if (argc < 4) {
printf("usage: %s <ip:port> num1 num2 /n", argv[0]);
exit(1);
} strcpy(server,argv[1]);
num1 = atoi(argv[2]);
num2 = atoi(argv[3]);
result = add(server, num1, num2, &sum); if (result != 0) {
printf("soap error, errcode=%d/n", result);
} else {
printf("%d + %d = %d/n", num1, num2, sum);
}
return 0;
}
注意:编译的时候我们须要gsoap包里的源码文件,把stdsoap2.c和stdsoap2.h文件复制到当前文件夹
6 makefile
root@ubuntu:/home/aries/Aries/gsoap# cat makefile
GSOAP_ROOT = /mnt/hgfs/E/gsoap_2.8.17/gsoap-2.8/gsoap
WSNAME = add
CC = g++ -g -DWITH_NONAMESPACES
INCLUDE = -I$(GSOAP_ROOT)
SERVER_OBJS = soapC.o stdsoap2.o soapServer.o $(WSNAME)server.o
CLIENT_OBJS = soapC.o stdsoap2.o soapClient.o $(WSNAME)client.o $(WSNAME)test.o all: server
server: $(SERVER_OBJS)
$(CC) $(INCLUDE) -o $(WSNAME)server $(SERVER_OBJS) client: $(CLIENT_OBJS)
$(CC) $(INCLUDE) -o $(WSNAME)test $(CLIENT_OBJS) cl:
rm -f *.o *.xml *.a *.wsdl *.nsmap soapH.h $(WSNAME)Stub.* $(WSNAME)server ns.xsd $(WSNAME)test
root@ubuntu:/home/aries/Aries/gsoap# make
g++ -g -DWITH_NONAMESPACES -c -o soapC.o soapC.c
g++ -g -DWITH_NONAMESPACES -c -o stdsoap2.o stdsoap2.c
g++ -g -DWITH_NONAMESPACES -c -o soapServer.o soapServer.c
g++ -g -DWITH_NONAMESPACES -c -o addserver.o addserver.c
g++ -g -DWITH_NONAMESPACES -I/mnt/hgfs/E/gsoap_2.8.17/gsoap-2.8/gsoap -o addserver soapC.o stdsoap2.o soapServer.o addserver.o
继续 make client
最后 文件例如以下:
add.add.req.xml addserver
makefile soapH.h stdsoap2.h
add.add.res.xml addserver.c soapC.c soapServer.c stdsoap2.o
addclient.c addserver.o soapClient.c soapServerLib.c
addclient.o addtest soapClientLib.c soapServer.o
add.h addtest.c soapClient.o soapStub.h
add.nsmap addtest.o soapC.o stdsoap2.c
效果:
Ubuntu 下的webservices的更多相关文章
- 在Ubuntu下搭建ASP.NET 5开发环境
在Ubuntu下搭建ASP.NET 5开发环境 0x00 写在前面的废话 年底这段时间实在太忙了,各种事情都凑在这个时候,没时间去学习自己感兴趣的东西,所以博客也好就没写了.最近工作上有个小功能要做成 ...
- 在Ubuntu下搭建Spark群集
在前一篇文章中,我们已经搭建好了Hadoop的群集,接下来,我们就是需要基于这个Hadoop群集,搭建Spark的群集.由于前面已经做了大量的工作,所以接下来搭建Spark会简单很多. 首先打开三个虚 ...
- Ubuntu下开启php调试模式,显示报错信息
在Ubuntu下php的缺省设置是不显示错误信息的,如果程序出错会显示“无法处理此请求的错误提示”,这在开发环境下非常不方便. 其实我们只要编辑下apache的配置文件就好 1.我的apache 配置 ...
- 在Ubuntu下安装ovs-dpdk
在Ubuntu下安装ovs-dpdk 参考资料:https://software.intel.com/zh-cn/articles/using-open-vswitch-with-dpdk-on-ub ...
- Ubuntu 下安装QT
Ubuntu 下安装QT 本文使用的环境 QT Library: qt-everywhere-opensource-src-4.7.4.tar.gz QT Creator: qt-creator-li ...
- Torch7在Ubuntu下的安装与配置
Torch7的本系列教程的主要目的是介绍Torch的入门使用.今天首先分享一下Torch7的安装.(在Ubuntu14.04安装torch7) 为什么选择Torch Torch的目标是在建立科学算法的 ...
- Ubuntu 下ibus拼音输入法启用 (ubuntu 16.04
Ubuntu 下ibus拼音输入法启用 我安装的是英文版的ubuntu 16.04,打开只带英文,并没有中文. 设置输入法为iBus 从system settings 进入language suppo ...
- Ubuntu下git的安装与使用
Ubuntu下git的安装与使用 Ubuntu下git的安装与使用与Windows下的大致相同,只不过个人感觉在Ubuntu下使用git更方便. 首先,确认你的系统是否已安装git,可以通过git指令 ...
- 在ubuntu下安装google chrome
由于手上有两台电脑,再加上我那个选择困难症加上纠结劲.所以果断把其中一台电脑只装linux系统,另一台电脑只装windows了.免得我老纠结!于是linux便选择了ubuntu. 由于浏览器一直用的是 ...
随机推荐
- Bzoj 3498 Cakes(三元环)
题面(权限题就不放题面了) 题解 三元环模板题,按题意模拟即可. #include <cstdio> #include <cstring> #include <vecto ...
- 洛谷——P2708 硬币翻转
P2708 硬币翻转 题目背景 难度系数:☆☆☆☆☆(如果你看懂了) 题目描述 从前有很多个硬币摆在一行,有正面朝上的,也有背面朝上的.正面朝上的用1表示,背面朝上的用0表示.现在要求从这行的第一个硬 ...
- 洛谷——P1920 成功密码
P1920 成功密码 题目描述 void_rank匪别人的书来看,原本想看杂志颓废的,结果不小心拿错拿成了被导师称作旁门左道的高中数学杂志<成功密码>.数学差得不行的void_rank实在 ...
- nyoj 151 Biorhythms
描述 Some people believe that there are three cycles in a person's life that start the day he or she i ...
- 【UOJ #179】线性规划 单纯形模板
http://uoj.ac/problem/179 终于写出来了单纯性算法的板子,抄的网上大爷的qwq 辅助线性规划找非基变量时要加个随机化才能A,我也不知道为什么,卡精度吗? 2017-3-6UPD ...
- HDU 5967 小R与手机(动态树)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5967 [题目大意] 给出一张图,每个点仅连一条有向边,或者不连, 要求查询在可更改有向边的情况每个 ...
- 【二分】【三分】【计算几何】XIII Open Championship of Y.Kupala Grodno SU Grodno, Saturday, April 29, 2017 Problem L. Lines and Polygon
题意:给你一个凸多边形,和多次询问,每次询问给你一条直线,问你这条直线与凸包上的顶点的最近距离是多少. 记当前询问的直线的斜率为K, 先找到与这条直线距离最远的两个点: 就把凸包所有的边当做有向直线进 ...
- js知识-进阶
1 DOM 1.1 查找标签 (1)直接查找 document.getElementById(“idname”) // dom对象 document.getElementsByTag ...
- CentOS通过日志反查入侵(转)
1.查看日志文件 Linux查看/var/log/wtmp文件查看可疑IP登陆 last -f /var/log/wtmp 该日志文件永久记录每个用户登录.注销及系统的启动.停机的事件.因此随着系统 ...
- IIS_右键点击浏览网站没有反应
现象: 点击浏览不会打开浏览器,没有任何反应 解决方法: 将IE设为默认浏览器即可