欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

GSOAP例子——calc

程序员文章站 2022-07-14 17:07: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)
相关标签: gsoap