2014-01-21 82 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選項。