You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by 蒋良 <li...@163.com> on 2012/03/31 07:48:31 UTC

how the server stop manually?

hello,
   how can I stop a server when a client has already connected? My operating step is below:
   1. I create a thread , in this thread call the function serve() of the Sever
   2. A client connected the Server
   3. In Main thread, I call the function stop() of the Server, but the Server has not stop and the thread I created not eixted. The thread is circulated in below code:
         for (;;) {
        if (eventHandler != NULL) {
          eventHandler->processContext(connectionContext, transport_);
        }
        if (!processor_->process(input_, output_, connectionContext) ||
            !input_->getTransport()->peek()) {
          break;
        }
      }
 
The Whole Server Code I Write is below
 
boost::shared_ptr<TThreadedServer> server1;
void ServerThread()
{
 try
 {
  TWinsockSingleton::create();
  int port = 9090;
  boost::shared_ptr<svr1Handler> handler(new svr1Handler());
  boost::shared_ptr<TProcessor> processor(new svr1Processor(handler));
  boost::shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
  boost::shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
  boost::shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
  server1 = boost::shared_ptr<TThreadedServer>(new TThreadedServer(processor,serverTransport,transportFactory,protocolFactory));
  server1->serve();
 }
 catch(TException &err)
 {
  
 }
 catch(...)
 {
  
 }
}
int main(int argc, char **argv) {
 boost::shared_ptr<boost::thread> svrThread;
 svrThread = boost::shared_ptr<boost::thread>(new boost::thread(ServerThread));
 boost::this_thread::sleep(boost::posix_time::seconds(10)); /// wait a client connected
 
 server1->stop();
 svrThread->join();  /// the thread can't return
 return 0;
}
 

 
liang.jiang