2014-01-21 51 views
1

即时通讯尝试构建多线程calC++ web服务。基于最初的例子。所以我想在我的二进制文件中构建SO_REUSEADDR。C++ gsoap SO_REUSEADDR

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; 
} 

如果我尝试建立这个:

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

我得到

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

肥皂结构是在stdsoap2.h

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

我是什么做错了? :<

回答

0

它取决于您使用soap2cpp生成器使用的选项。

用-i选项CalculatorService的从肥皂结构继承,那么你应该使用:

​​

随着-j选项CalculatorService的含有皂结构,那么你应该使用:

c.soap->bind_flags |= SO_REUSEADDR; 

看来你考虑到CalculatorService包含soap结构,请使用-i选项。