title: gSOAP Chaining C++ Server Classes to Accept Messages on the Same Port Not Working
body: <p>We have six WSDLs compiled within the same project, and due to some limits of hardware we can only open one port for listening. </p>
  
  <p>For doing this, we choose the approach described by chapter <strong>7.2.8  How to Chain C++ Server Classes to Accept Messages on the Same Port</strong> in this <a href="http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc7.2.8">gSOAP Manual</a>.</p>
  
  <p>However, when using this approach in we encounter many sever issues:</p>
  
  <p><strong>1. If lots of requests arrive concurrently, then sometimes soap_begin_serve reports error with error=-1, socket is closed immediately by soap server after it is established</strong> </p>
  
  <p><strong>2. If we call xxx.destory() after soap_free_stream(), then soap_accept() will 
  report an error of bad file descriptor and not work anymore (solved)</strong>      </p>
  
  <p>Anybody knows what are the reasons of above phoenomenon? How to solve them?<br>
  Our code is very close to the example except a few changes, see below section.</p>
  
  <pre><code>//server thread 
  Abc::soapABCService server; // generated with soapcpp2 -i -x -n -QAbc
  
  server.bind(NULL, 12345, 100);  
  server.soap_mode = SOAP_KEEP_ALIVE | SOAP_UTF_CSTRING;
  server.recv_timeout = server.send_timeout = 60;
  
  while (true)
  {
      server.accept(); 
  
      ... 
      pthread_create(&amp;pid, NULL, handle_new_request, server.copy());
   }  // while(true)
  
  
  
   // work thread - the thread function
   void *handle_new_request(void* arg)
   {
      // generated with soapcpp2 -i -x -n -QAbc 
      Abc::soapABCService *abc = (Abc::soapABCService*)arg; 
      Uvw::soapUVWService uvw; // generated with soapcpp2 -i -x -n -QUvw 
      Xyz::soapXYZService xyz; // generated with soapcpp2 -i -x -n -QXyz
  
      if(soap_begin_serve(abc)) 
      {
          //sometimes it reports error
          //due to unkown reason, socket was closed by soap server 
          abc-&gt;soap_stream_fault(std::cerr); 
      }
      else if (abc-&gt;dispatch() == SOAP_NO_METHOD) 
      { 
          soap_copy_stream(&amp;uvw, abc);
          uww.state = SOAP_COPY;
  
          if (uvw.dispatch() == SOAP_NO_METHOD) 
          { 
              soap_copy_stream(&amp;xyz, &amp;uvw); 
              xyz.state = SOAP_COPY;
  
              if (xyz.dispatch()) 
              { 
                  soap_send_fault(&amp;xyz); // send fault to client 
                  xyz.soap_stream_fault(std::cerr); 
              } 
  
              soap_free_stream(&amp;xyz); // free the copy 
              xyz.destroy();
          } 
          else
          { 
              soap_send_fault(&amp;uvw); // send fault to client 
              uvw.soap_stream_fault(std::cerr); 
          }  
  
          soap_free_stream(&amp;uvw); // free the copy 
          uvw.destroy();
      }
      else if (abc-&gt;error)
      {
          abc-&gt;soap_stream_fault(std::cerr);
      }
      else
      {
  
      }
  
      abc-&gt;destroy();
      delete abc;
      abc = NULL;
   }       
  </code></pre>