(2) 远程函数的调用必须返回一个errocode 例如:SOAP_OK SOAP_FAULT 等
SOAP 1.1The exception description can be assigned to the soap->fault->faultstring string and details can be assigned to the soap->fault->detail string
SOAP1.2 requires the soap->fault->SOAP ENV Reason and the soap->fault->SOAP ENV Detail strings to be assigned.
服务端用soap receiver fault 返回相应的错误信息:
return soap receiver fault(soap, ”Square root of negative number”, ”I can only take the square root of a non-negative number”);
// Contents of file ”calc.cpp”:
#include ”soapH.h”
#include <math.h> // for sqrt()
main()
{ soap serve(soap new()); // use the remote method request dispatcher
}// Implementation of the ”add” remote method:
int ns add(struct soap *soap, double a, double b, double &result)
{ result = a + b;
return SOAP OK;
}// Implementation of the ”sub” remote method:
int ns sub(struct soap *soap, double a, double b, double &result)
{ result = a - b;
return SOAP OK;
}// Implementation of the ”sqrt” remote method:
int ns sqrt(struct soap *soap, double a, double &result)
{ if (a >= 0)
{ result = sqrt(a);
return SOAP OK;
}else
return soap receiver fault(soap, ”Square root of negative number”, ”I can only take the square
root of a non-negative number”);
}// As always, a namespace mapping table is needed:
struct Namespace namespaces[] =
{ // {”ns-prefix”, ”ns-name”}
{”SOAP-ENV”, ”http://schemas.xmlsoap.org/soap/envelope/”},
{”SOAP-ENC”, ”http://schemas.xmlsoap.org/soap/encoding/”},
{”xsi”, ”http://www.w3.org/2001/XMLSchema-instance”},
{”xsd”, ”http://www.w3.org/2001/XMLSchema”},
{”ns”, ”urn:simple-calc”}, // bind ”ns” namespace prefix
{NULL, NULL}
};
(3)
int main()
{ struct soap soap;
int m, s; // master and slave sockets
soap init(&soap);
m = soap bind(&soap, "machine.cs.fsu.edu", 18083, 100);
if (m < 0)
soap print fault(&soap, stderr);
else
{ fprintf(stderr, "Socket connection successful: master socket = %d/n", m);
for (int i = 1; ; i++)
{ s = soap accept(&soap);
if (s < 0)
{ soap print fault(&soap, stderr);
break;
}fprintf(stderr, "%d: accepted connection from IP=%d.%d.%d.%d socket=%d", i,
(soap.ip>>24)&0xFF, (soap.ip>>16)&0xFF, (soap.ip>>8)&0xFF, soap.ip&0xFF, s);
if (soap serve(&soap) != SOAP OK) // process RPC request
soap print fault(&soap, stderr); // print error
fprintf(stderr, "request served/n");
soap destroy(&soap); // clean up class instances
soap end(&soap); // clean up everything and close socket
}
}soap done(&soap); // close master socket and detach environment
}
Function Description
soap new() Allocates and Initializes gSOAP context
soap init(struct soap *soap) Initializes a stack-allocated gSOAP context (required
once)
soap bind(struct soap *soap, char *host, int port,int backlog) Returns master socket (backlog = max. queue size for requests). When host==NULL: host is the machine on which the service runs
soap accept(struct soap *soap) Returns slave socket
soap end(struct soap *soap) Clean up deserialized data (except class instances) and temporary data
soap free temp(struct soap *soap) Clean up temporary data only
soap destroy(struct soap *soap) Clean up deserialized class instances (note: thisfunction will be renamed with option -n
soap done(struct soap *soap) Reset and detach context: close master/slave sockets and remove callbacks
soap free(struct soap *soap) Detach and deallocate context (soap new())