我正在尝试建立一个多线程的网络服务。单线程是有效的,在我的main函数中,我使用以下代码:
int main(int argc, char **argv) {
CardSoapBindingService CardSrvc;
Config Conf ;
Conf.update();
int port = Conf.listener_port;
if (!port)
CardSrvc.serve();
else {
if (CardSrvc.run(port)) {
CardSrvc.soap_stream_fault(std::cerr);
exit(-1);
}
}
return 0;
}但是我想要多线程,所以我在文档中找到了他们的example,我用它代替了我的代码。在编译时,我得到了这个错误:
main.cpp: In function int main(int, char**)': main.cpp:56: error:soap_serve‘未声明(首先使用此函数)
main.cpp:56: error:(每个未声明的标识符对于出现在其中的每个函数只报告一次。)
main.cpp: In function void* process_request(void*)':<br> main.cpp:101: error:soap_serve‘未声明(首先使用此函数)
make:* main.o Fehler 1
我怎么才能让它正常工作呢?
发布于 2011-11-24 00:06:33
重要信息:
这段代码至少需要gsoap版本2.8.5。它最初是在Solaris8和gsoap版本2.8.3上构建的,将代码移植到Ubuntu并在valgrind下运行表明,2.8.3 gsoap++库破坏了内存,从而导致了SIGSEGV。应该注意的是,在25/11/11,Ubuntu使用apt-get安装的gsoap版本是坏的2.8.3。需要手动下载并构建最新版本的gsoap (确保在配置gsoap构建之前安装flex和bison!)。
使用gsoap 2.8.5,下面的代码愉快地创建线程并为多个客户端提供SOAP消息,valgrind现在报告内存分配错误为0。
查看您的代码,您正在使用的示例是使用-i (或-j)选项构建的,用于创建C++对象。gsoap中的线程示例是用标准C编写的;因此引用soap_serve()之类的函数是您没有的。
下面是我快速重写的多线程示例,以使用生成的C+对象。它基于以下定义文件:
// Content of file "calc.h":
//gsoap ns service name: Calculator
//gsoap ns service style: rpc
//gsoap ns service encoding: encoded
//gsoap ns service location: http://www.cs.fsu.edu/~engelen/calc.cgi
//gsoap ns schema namespace: urn:calc
//gsoap ns service method-action: add ""
int ns__add(double a, double b, double &result);
int ns__sub(double a, double b, double &result);
int ns__mul(double a, double b, double &result);
int ns__div(double a, double b, double &result);然后,主服务器代码如下所示:
#include "soapCalculatorService.h" // get server object
#include "Calculator.nsmap" // get namespace bindings
#include <pthread.h>
void *process_request(void *calc) ;
int main(int argc, char* argv[])
{
CalculatorService c;
int port = atoi(argv[1]) ;
printf("Starting to listen on port %d\n", port) ;
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;
}
}
void *process_request(void *calc)
{
pthread_detach(pthread_self());
CalculatorService *c = static_cast<CalculatorService*>(calc) ;
c->serve() ;
c->destroy() ;
delete c ;
return NULL;
} 这是一个非常基本的线程模型,但是它展示了如何使用gsoap生成的C++类来构建多线程服务器。
https://stackoverflow.com/questions/8150380
复制相似问题