Delayed Stock Quote Example

This example shows you how to create a client that will interact with Xmethod's Delayed Stock Quote service to provide you with up to date information on your choice of stock.

The client program will accept the ticker name of a stock (e.g. AOL) from the command line and will display it's current value.

Step 1: Write the Main Program

Save the program source below to a local file (e.g. quote.c). To do this, you can also click here and select "File - Save As" from your browser's menu.
 
#include "soapH.h"    // include the generated proxy
void main(int argc, char** argv){
  struct soap *soap = soap_new();
  float Result;       // to hold the stock's current value
  char *sym;          // to hold the ticker's name
  if(argc > 1 )       // ticker name must be supplied on the command line
      sym = argv[1];
  else
      return -1;

  if (soap_call_ns__getQuote(soap, "services.xmethods.net/soap/", "", sym, &Result) == 0)
    printf("\nCompany - %s    Quote- %f \n" , sym, Result);
  else
    soap_print_fault(soap, stderr);
}

// Every client (and server) application MUST specify a table with namespace info.
// The first four namespaces are standard and remain unchanged until newer schema versions appear.
// The ns namespace is the delayed stock quote namespace.
// The name "ns" MUST correspond to the prefix of "ns__getQuote" function prototype (see below).
struct Namespace namespaces[] =
{
  { "SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/" },
  { "SOAP-ENC","http://schemas.xmlsoap.org/soap/encoding/"},
  { "xsi", "http://www.w3.org/1999/XMLSchema-instance" },
  { "xsd", "http://www.w3.org/1999/XMLSchema" },
  { "ns", "urn:xmethods-delayed-quotes" },
  { NULL, NULL }    // end of table
};

Step 2: Generate the SOAP Stub Routines

Before you can compile and run your delayed stock quote program, you must generate C/C++ sources using our stub compiler. The generated sources take care of the SOAP serialization and deserialization of the data required to access the SOAP service.

The input to our stub compiler is a specification of the name of the SOAP method (with it's optional namespace) and the data structures given as C/C++ declarations. The gSOAP wsdl2h WSDL parser creates this header file from the WSDL of the service:

wsdl2h -o quote.h http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsd
For this example, the quote.h file contains a function prototype of the service method along with some Web service details:
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service namespace: urn:xmethods-delayed-quotes
//gsoap ns service location: http://services.xmethods.net/soap
//gsoap ns service method-action: getQuote ""
int ns__getQuote(char *symbol, float *Result);
This is sufficient to generate all the C/C++ sources necessary to build your ticker application!

The method name is "getQuote" and "symbol" and "Result" are argument names that correspond to the request and response XML schema definitions for the getQuote method, respectively. (Note: the prefix "ns__" to the getQuote method can be omitted but is useful as this will provide the namespace identification for invoking Xmethod's getQuote method)

To generate the sources, click here to use our SOAP C++ Stub Compiler or run the compiler on your machine by entering:

soapcpp2 quote.h
(You can download the compiler). This will produce a web page containing the C/C++ sources for:
soapStub.h
soapH.h
soapC.cpp
soapClient.cpp
soapServer.cpp
You need to save these sources to your local drive under the file names as indicated.

Two files are not generated but are required to build your application: stdsoap2.h, and stdsoap2.cpp. Save these files to your local drive too.

Step 3: Compiling Your Client App

Compile the sources. For example, in Linux the command to compile would be:
g++ -o quote quote.c soapC.cpp soapClient.cpp stdsoap2.cpp

Step 4: Try it Out

Execute the command "quote AOL" which returns with
Company - AOL    Quote - 49.8
(as of 6/18/2001.)