2
2
xml-rpc-currentTimeServer.cpp
4
4
XML-RPC currenTime server (C++ version)
5
CGI or stand-alone multi-threaded server
6
7
Returns XML-RPC message with current time to client.
10
cc xml-rpc-currentTimeServer.cpp xml-rpc.cpp xml-rpc-io.cpp stdsoap2.cpp soapC.cpp
11
cc -o xml-rpc-currentTimeServer xml-rpc-currentTimeServer.cpp xml-rpc.cpp xml-rpc-io.cpp stdsoap2.cpp soapC.cpp
12
Install as CGI on Web server
13
Or run as stand-alone server (e.g. on port 18000):
14
./xml-rpc-currentTimeServer 18000
12
16
--------------------------------------------------------------------------------
13
17
gSOAP XML Web services tools
41
45
#include "xml-rpc-io.h"
49
#include <pthread.h> // use Pthreads
52
#define BACKLOG (100) // Max. request backlog
53
#define MAX_THR (8) // Max. threads to serve requests
43
55
using namespace std;
45
57
int serve_request(soap*);
47
59
int main(int argc, char **argv)
49
soap *ctx = soap_new();
61
soap *ctx = soap_new1(SOAP_IO_KEEPALIVE | SOAP_XML_INDENT);
62
ctx->send_timeout = 5; // 5 sec
63
ctx->recv_timeout = 5; // 5 sec
52
66
return serve_request(ctx);
54
68
int port = atoi(argv[1]);
56
if (!soap_valid_socket(soap_bind(ctx, NULL, port, 100)))
74
if (!soap_valid_socket(soap_bind(ctx, NULL, port, BACKLOG)))
58
76
soap_print_fault(ctx, stderr);
78
100
int serve_request(soap* ctx)
102
fprintf(stderr, "Started... ");
80
104
methodCall m(ctx);
82
if (m.recv() != SOAP_OK)
83
soap_print_fault(ctx, stderr);
106
#ifdef _POSIX_THREADS
107
pthread_detach(pthread_self());
110
// HTTP keep-alive max number of iterations
111
unsigned int k = ctx->max_keep_alive;
86
methodResponse r(ctx);
115
if (ctx->max_keep_alive > 0 && !--k)
118
if (m.recv() != SOAP_OK)
119
soap_print_fault(ctx, stderr);
122
methodResponse r(ctx);
88
124
if (!strcmp(m.name(), "currentTime.getCurrentTime"))
89
// method name matches: first parameter of response is time
92
// otherwise, set fault
93
r.set_fault("Wrong method");
95
if (r.send() != SOAP_OK)
96
soap_print_fault(ctx, stderr);
99
// close (but keep-alive keeps socket open)
125
// method name matches: first parameter of response is time
128
// otherwise, set fault
129
r.set_fault("Wrong method");
131
if (r.send() != SOAP_OK)
132
soap_print_fault(ctx, stderr);
134
// close (keep-alive may keep socket open when client supports it)
137
} while (ctx->keep_alive);
139
int err = ctx->error;
103
142
soap_destroy(ctx);
145
#ifdef _POSIX_THREADS
146
// free the ctx copy for this thread
150
fprintf(stderr, "done\n");
109
155
/* Don't need a namespace table. We put an empty one here to avoid link errors */