gsoap and arrays - the easy way to understand

What is gsoap ? - The sample project : gsoap (with array of xsd__string)


What is gsoap ?

Instead copy and paste from the official website, i just put a link to it :

gSOAP: Generator Tools for Coding SOAP/XML Web Services in C and C++


The sample project : gsoap (with array of xsd__string)

When i have searched for dynamic arrays and gsoap, i had not found a clear sample, so i put mine here, and i hope it can help others.

Somes infos : Official page for gSOAP and Dynamic Arrays

I had used a array of xsd:string (xsd__string)

 

atserver.c

// --------------------------------------------------------------
#include "at.nsmap"

#define _LISTEN_PORT_	749

// --------------------------------------------------------------
// --------------------------------------------------------------
int main()
{
        struct soap v_soap;
	
        soap_init(&v_soap);
        if(soap_bind(&v_soap, NULL, _LISTEN_PORT_, 100) < 0 ) return -1;

        for(;;)
        {
                v_soap.accept_timeout = 1;
                if(soap_accept(&v_soap) < 0)
		{
			// soap_print_fault(&v_soap, stderr);
			continue;
		}

                soap_serve(&v_soap);
                soap_end(&v_soap);
        }

        soap_done(&v_soap);

	exit(EXIT_SUCCESS);
}

// --------------------------------------------------------------
// --------------------------------------------------------------
int ns__atTestArrayOfString(struct soap *p_soap, xsd__string cmd, struct ns__CharArray *aString)
{
	int     ret = SOAP_OK;

	fprintf(stderr, "%s[%d] - Ok ! (domaine: %s)\n", __FILE__, __LINE__, cmd);
	
	aString->__size = 3;
	aString->__ptr = malloc( (aString->__size + 1) * sizeof(*aString->__ptr) );
	aString->__ptr[0] = (char *) malloc( (2+1) * sizeof(char)); strcpy(aString->__ptr[0], "a1");
	aString->__ptr[1] = (char *) malloc( (2+1) * sizeof(char)); strcpy(aString->__ptr[1], "b2");
	aString->__ptr[2] = (char *) malloc( (2+1) * sizeof(char)); strcpy(aString->__ptr[2], "c3");
	aString->__ptr[aString->__size] = NULL;
	
	return ret;
}

atclient.c

#include "at.nsmap"

int main(int argc, char *argv[])
{
	unsigned int i;
        struct soap soap;
	char buf[1024];
	struct ns__CharArray l;
	
	if(argc<2)
	{
		fprintf(stderr,"%s[%d] atclient failed !, usage :\n%s \n", __FILE__, __LINE__, argv[0]);
		exit(EXIT_FAILURE);
	}
	
	soap_init(&soap);
	snprintf(buf, sizeof(buf)-1, "http://%s:749/", argv[1]);
	fprintf(stderr,"%s[%d] atclient: host:%s\n", __FILE__, __LINE__, argv[1]);

	if(soap_call_ns__atTestArrayOfString(&soap, buf, "", argv[0], &l) == SOAP_OK)
	{
		fprintf(stderr, "ok\n\n");
		for(i=0; i < l.__size; i++)
		{
			fprintf(stderr, "i: %d, l: <%s>\n", i, l.__ptr[i]);
		}
		
	}
	else
	{
		soap_print_fault(&soap, stderr);
	}
	soap_end(&soap);
	soap_done(&soap); 
	
	exit(EXIT_SUCCESS);
}

at.h

//gsoap ns service name: at
//gsoap ns service style: rpc
//gsoap ns service location: http://localhost:749
//gsoap ns schema namespace: urn:at
//gsoap ns service encoding: encoded

typedef char *xsd__string;

struct ns__CharArray
{
	xsd__string     *__ptr;         // pointer to array of string
	int             __size;         // array size
};

int ns__atTestArrayOfString(xsd__string cmd, struct ns__CharArray *aString);

Makefile

CC=gcc
GSOAPDIR=../gsoap/gsoap-linux-2.7
SOAPCPP=$(GSOAPDIR)/bin/soapcpp2
INCLUDED=-I$(GSOAPDIR)
CFLAGS=-W -Wall $(INCLUDED)
LDFLAGS=$(CFLAGS)
SOAPSCODE=$(GSOAPDIR)/stdsoap2.o soapC.o soapServer.o
SOAPCCODE=$(GSOAPDIR)/stdsoap2.o soapC.o soapClient.o
EXEC=atserver atclient

all: soapstruct
	$(MAKE) $(EXEC)

atserver: atserver.o $(SOAPSCODE)
	$(CC) -o $@ $^ $(LDFLAGS)

atclient: atclient.o $(SOAPCCODE)
	$(CC) -o $@ $^ $(DJBLIB) $(LDFLAGS)

%.o: %.c
	$(CC) -o $@ -c $< $(CFLAGS)

clean:
	rm -f $(EXEC)
	rm soapC.c soapClient.c soapClientLib.c soapH.h soapServer.c soapServerLib.c soapStub.h
	rm *.o *.xml *.xsd *.nsmap *.wsdl

soapstruct:
	$(SOAPCPP) -c at.h


David CHANIAL - Homepage : OpenSource