首页 技术 正文
技术 2022年11月18日
0 收藏 735 点赞 3,716 浏览 2914 个字

1. gsoap的好处就不用说了:百度百科

2. gsoap的下载地址:项目地址,目前我使用的是2.8.15版本

3. 开发环境:Ubuntu13.10

4. 具体操作步骤(以简单相加为例):

  1) 编写add.h(头文件)

//gsoap ns service name: calc
//gsoap ns service protocol: SOAP
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: http://localhost:8888
//gsoap ns service location: http://localhost:8888
//gsoap ns service port: http://localhost:8888int ns__add( int num1, int num2, int* sum );

  2) 编写addserver.c(服务器)

#include "soapH.h"
#include "calc.nsmap" /* 与add.h的第一行命名空间(ns)有关 */
#include <stdio.h>
#include <stdlib.h>
int
main (int argc, char *argv[])
{
int m, s;
struct soap add_soap;
soap_init (&add_soap);
if (argc < )
{
printf ("usage: %s <server_port> \n", argv[]);
exit ();
}
else
{
m = soap_bind (&add_soap, NULL, atoi (argv[]), );
if (m < )
{
soap_print_fault (&add_soap, stderr);
exit (-);
}
fprintf (stderr, "Socket connection successful: master socket = %d\n",m);
for (;;)
{
s = soap_accept (&add_soap);
if (s < )
{
soap_print_fault (&add_soap, stderr);
exit (-);
}
fprintf (stderr,
"Socket connection successful: slave socket = %d\n", s);
soap_serve (&add_soap); //该句说明该server的服务
soap_end (&add_soap);
}
}
return ;
}int
ns__add (struct soap *add_soap, int num1, int num2, int *sum)
{
*sum = num1 + num2;
return ;
}

  3) 编写addclient.c(客户端)

#include "soapH.h"
#include "calc.nsmap" /* 与add.h的第一行命名空间(ns)有关 */
#include <stdio.h>
#include <stdlib.h>int add (const char *server, int num1, int num2, int *sum);int
main (int argc, char **argv)
{
int result = -;
char *server = "http://localhost:4567"; /* 定义server */
int num1 = ;
int num2 = ;
int sum = ;
if (argc < ) /* 判断传入参数 */
{
printf ("usage: %s num1 num2 \n", argv[]);
exit ();
}
num1 = atoi (argv[]);
num2 = atoi (argv[]);
result = add (server, num1, num2, &sum); /* 执行add() */
if (result != ) /* 输出result */
{
printf ("soap err,errcode = %d\n", result);
}
else
{
printf ("%d+%d=%d\n", num1, num2, sum);
}
return ;
}int
add (const char *server, int num1, int num2, int *sum)
{
struct soap add_soap; /* 创建add_soap()结构体 */
int result = ;
soap_init (&add_soap); /* soap_init()*/
soap_call_ns__add (&add_soap, server, "", num1, num2, sum); /* 调用soap_call_ns_add() */
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;
}

  4) 编写Makefile文件

# this is a Makefile to build client and server
# please setting the GSOAP_ROOT first.
# build procedure:
# step1: make soap
# step2: make allOBJ_NS := calc
OBJ_NAME := add
GSOAP_ROOT := /home/scue/work/gsoap_2.8.15/gsoap
INCLUDE := -I$(GSOAP_ROOT)CC := clang
GCC := clang
#CC := arm-linux-gcc
#GCC := arm-linux-gccOBJ_SERVER := soapC.o stdsoap2.o soapServer.o $(OBJ_NAME)server.o
OBJ_CLIENT := soapC.o stdsoap2.o soapClient.o $(OBJ_NAME)client.oall: server client server: $(OBJ_SERVER)
$(CC) $(INCLUDE) $^ -o $@
client: $(OBJ_CLIENT)
$(CC) $(INCLUDE) $^ -o $@.PHONY:soap
soap:
@cp -v $(GSOAP_ROOT)/stdsoap2.* .
@$(GSOAP_ROOT)/bin/linux386/soapcpp2 -c $(OBJ_NAME).h
# -c 表示生成c文件.PHONY:clean
clean:
rm -f server client *.o
distclean:
rm -f server client *.o ns* soap* *.xml *.nsmap *.wsdl stdsoap2.*

  5) 编译及验证

make soap && make
./server
./client 3 #将会返回5,也可以直接在浏览器中输入http://localhost:4567进行验证

参考文章:

  1.http://blog.csdn.net/wesleyluo/article/details/5532633

上一篇: rEFind 教程
下一篇: ORA-27090 故障一例
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,083
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,558
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,407
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,180
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,816
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,899