当前位置:网站首页>gSoap例子——calc
gSoap例子——calc
2022-06-29 09:10:00 【水似冰】
一、readme
Simple calculator service implements:
- add(a,b)
- sub(a,b)
- mul(a,b)
- div(a,b)
- pow(a,b)
Compilation in C (see samples/calc):
soapcpp2 -c calc.h
cc -o calcclient calcclient.c stdsoap2.c soapC.c soapClient.c
cc -o calcserver calcserver.c stdsoap2.c soapC.c soapServer.c
二、calc.h
int ns__add(double a, double b, double *result);
//gsoap ns service method: sub Subtracts two values
int ns__sub(double a, double b, double *result);
//gsoap ns service method: mul Multiplies two values
int ns__mul(double a, double b, double *result);
//gsoap ns service method: div Divides two values
int ns__div(double a, double b, double *result);
//gsoap ns service method: pow Raises a to b
int ns__pow(double a, double b, double *result);
三、calcclient.c和calcserver.c
1.calcclient.c
/*
calcclient.c
Example calculator service client in C
Compilation in C (see samples/calc/calc.h):
$ soapcpp2 -c calc.h
$ cc -o calcclient calcclient.c stdsoap2.c soapC.c soapClient.c
where stdsoap2.c is in the 'gsoap' directory, or use libgsoap:
$ cc -o calcclient calcclient.c soapC.c soapClient.c -lgsoap
--------------------------------------------------------------------------------
gSOAP XML Web services tools
Copyright (C) 2001-2008, Robert van Engelen, Genivia, Inc. All Rights Reserved.
This software is released under one of the following two licenses:
Genivia's license for commercial use.
--------------------------------------------------------------------------------
Product and source code licensed by Genivia, Inc., [email protected]
--------------------------------------------------------------------------------
*/
#include "soapH.h"
#include "calc.nsmap"
const char server[] = "http://websrv.cs.fsu.edu/~engelen/calcserver.cgi";
/* = "http://localhost:8080"; to test against samples/webserver */
int main(int argc, char **argv)
{ struct soap soap;
double a, b, result;
if (argc < 4)
{ fprintf(stderr, "Usage: [add|sub|mul|div|pow] num num\n");
exit(0);
}
soap_init1(&soap, SOAP_XML_INDENT);
a = strtod(argv[2], NULL);
b = strtod(argv[3], NULL);
switch (*argv[1])
{ case 'a':
soap_call_ns__add(&soap, server, "", a, b, &result);
break;
case 's':
soap_call_ns__sub(&soap, server, "", a, b, &result);
break;
case 'm':
soap_call_ns__mul(&soap, server, "", a, b, &result);
break;
case 'd':
soap_call_ns__div(&soap, server, "", a, b, &result);
break;
case 'p':
soap_call_ns__pow(&soap, server, "", a, b, &result);
break;
default:
fprintf(stderr, "Unknown command\n");
exit(0);
}
if (soap.error)
soap_print_fault(&soap, stderr);
else
printf("result = %g\n", result);
soap_destroy(&soap);
soap_end(&soap);
soap_done(&soap);
return 0;
}
2.calcserver.c
/*
calcserver.c
......
*/
#include "soapH.h"
#include "calc.nsmap"
int main(int argc, char **argv)
{ SOAP_SOCKET m, s; /* sockets */
struct soap soap;
soap_init(&soap);
if (argc < 2)
soap_serve(&soap); /* serve as CGI application */
else
{ m = soap_bind(&soap, NULL, atoi(argv[1]), 100);
if (!soap_valid_socket(m))
{ soap_print_fault(&soap, stderr);
exit(1);
}
fprintf(stderr, "Socket connection successful: master socket = %d\n", m);
for ( ; ; )
{ s = soap_accept(&soap);
fprintf(stderr, "Socket connection successful: slave socket = %d\n", s);
if (!soap_valid_socket(s))
{ soap_print_fault(&soap, stderr);
exit(1);
}
soap_serve(&soap);
soap_end(&soap);
}
}
return 0;
}
int ns__add(struct soap *soap, double a, double b, double *result)
{ (void)soap;
*result = a + b;
return SOAP_OK;
}
int ns__sub(struct soap *soap, double a, double b, double *result)
{ (void)soap;
*result = a - b;
return SOAP_OK;
}
int ns__mul(struct soap *soap, double a, double b, double *result)
{ (void)soap;
*result = a * b;
return SOAP_OK;
}
int ns__div(struct soap *soap, double a, double b, double *result)
{ if (b)
*result = a / b;
else
{ char *s = (char*)soap_malloc(soap, 1024);
(SOAP_SNPRINTF(s, 1024, 100),
"<error xmlns=\"http://tempuri.org/\">Can't divide %f by %f</error>",
a, b);
return soap_sender_fault(soap, "Division by zero", s);
}
return SOAP_OK;
}
int ns__pow(struct soap *soap, double a, double b, double *result)
{ *result = pow(a, b);
if (soap_errno == EDOM) /* soap_errno is like errno, but compatible with Win32 */
{ char *s = (char*)soap_malloc(soap, 1024);
(SOAP_SNPRINTF(s, 1024, 100),
"<error xmlns=\"http://tempuri.org/\">Can't raise %f to %f</error>",
a, b);
return soap_sender_fault(soap, "Power function domain error", s);
}
return SOAP_OK;
}
四、使用与总结
4.1、步骤
1、在服务端先运行:
./calcserver
2、在客户端运行
./calcclient add 1 2
3、结果输出:
result = 3
4.2、总结
- client端程序运行可把前几个参数传入server端
- client端程序想带从server出的参数一定要放在最后一个参数,gsoap规定的
- server端的程序是自己按照需求去实现的,但是切记加入第一参数
fun(struct soap *soap,x,y)
边栏推荐
- 阿里云服务器安装配置redis,无法远程访问
- 遍历vector容器中的对象的方式
- After installing anaconda, you need to enter a password to start jupyterlab
- Reading notes on how to connect the network - Web server request and response (V)
- IDEA调试失败,报JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_LOAD(196)
- Have you done the network security "physical examination" this year?
- linux环境下安装配置redis,并设置开机自启动
- CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
- MySQL modify auto increment initial value
- 数据仓库:金融/银行业的分层架构篇
猜你喜欢

基于PyQt5和Qt Designer的简易加法计算器的制作

Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail

UE4 compile a single file (VS and editor start respectively)

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

GD32F4xx 以太网芯片(enc28j60)驱动移植

UE4 blueprint modify get a copy in array to reference

Hystrix熔断器:服务熔断与服务降级

Print service IP setting scheme

Five heart charity matchmaker team

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
随机推荐
长安链GO语言智能合约环境搭建及使用
装饰器模式的应用,包装ServletRequest,增加addParameter方法
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
自定义mvc框架实现
GD32F4xx 以太网芯片(enc28j60)驱动移植
2020-09-18 referer认证 url转义
通用分页框架
指针函数和函数指针
KiCad学习笔记--快捷键
MySQL configuring master-slave databases
Western Polytechnic University, one of the "seven national defense schools", was attacked by overseas networks
MySQL modify auto increment initial value
请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
Basic operations of MAC MySQL database
1424. 对角线遍历 II
Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear
Student增删gaih
Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
券商经理给的开户二维码办理股票开户安全吗?我想开个户
UE4 material UV texture does not stretch with model scale