简体   繁体   中英

c++ gsoap SO_REUSEADDR

im trying to build a multithreaded calc++ webservice. On basis of the original example. So i wanted to build the SO_REUSEADDR in my binary.

int main(int argc, char* argv[])
{
    CalculatorService c;
    int port = atoi(argv[1]) ;
    printf("Starting to listen on port %d\n", port) ;
    c.soap->bind_flags |= SO_REUSEADDR;
    if (soap_valid_socket(c.bind( NULL, port, 100)))
    {
        CalculatorService *tc ;
        pthread_t tid;
        for (;;)
        {
            if (!soap_valid_socket(c.accept()))
                return c.error;
            tc = c.copy() ; // make a safe copy
            if (tc == NULL)
                break;
            pthread_create(&tid, NULL, (void*(*)(void*))process_request, (void*)tc);

            printf("Created a new thread %ld\n", tid) ;
        }
    }
    else 
    {
        return c.error;
    }
    printf("hi");
}


void *process_request(void *calc)
{
   pthread_detach(pthread_self());
   CalculatorService *c = static_cast<CalculatorService*>(calc) ;
   c->serve() ;
   c->destroy() ;
   delete c ;
   return NULL;
}

If i try to build this with:

g++  -o calcmulti main.cpp stdsoap2.cpp soapC.cpp soapCalculatorService.cpp -lpthread

I get

main.cpp: In function 'int main(int, char**)':
main.cpp:13: error: invalid use of 'struct soap'

The soap struct is in the stdsoap2.h

struct SOAP_STD_API soap
{
    int bind_flags;               /* bind() SOL_SOCKET sockopt flags, e.g. set to SO_REUSEADDR to enable reuse */
}

What am i doing wrong? :<

It depends options you used using the soap2cpp generator.

With -i option CalculatorService inherit from the soap structure then you should use :

 c.bind_flags |= SO_REUSEADDR;

With -j option CalculatorService contains a soap structure then you should use :

 c.soap->bind_flags |= SO_REUSEADDR;

It seems you use the -i option considering CalculatorService contains a soap structure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM