【发布时间】:2015-06-04 21:03:35
【问题描述】:
我正在使用 Apache Thrift 来设置 C++ 服务器。在服务器端我要执行
execl("a.out","",(char *)NULL);
但是要么先执行上面的代码行,要么先执行 server.serve()。有什么方法可以让服务器启动并让这行代码在服务器之上运行?
我尝试将它放在 message_test 函数中,但我只希望它执行一次,而不是每次启动客户端时。
a.cpp:
int main(){
cout<<"I have started up"<<endl;
string message;
while (cin >> message){
cout<<"your message is: "<<message<<endl;
}
cout<<"I have now exited"<<endl;
}
服务器.cpp
class ThforkServiceHandler : virtual public ThforkServiceIf {
public:
ThforkServiceHandler() {
// Your initialization goes here
}
void message_test(const std::string& message) {
// Your implementation goes here
printf("message_test\n");
}
};
int main(int argc, char **argv) {
int port = 9090;
shared_ptr<ThforkServiceHandler> handler(new ThforkServiceHandler());
shared_ptr<TProcessor> processor(new ThforkServiceProcessor(handler));
shared_ptr<TServerTransport> serverTransport(new TServerSocket(port));
shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory());
shared_ptr<TProtocolFactory> protocolFactory(new TBinaryProtocolFactory());
TSimpleServer server(processor, serverTransport, transportFactory, protocolFactory);
server.serve();
return 0;
}
【问题讨论】: