Ste*_*eve 5 c++ linux multithreading gsoap
我们在同一个项目中编译了六个WSDL,由于硬件的限制,我们只能打开一个端口进行监听.
为此,我们选择本" gSOAP手册"第7.2.8章如何链接C++服务器类以接受同一端口上的消息所描述的方法.
但是,在使用此方法时,我们会遇到许多严重问题:
1.如果大量请求同时到达,那么有时soap_begin_serve报告错误,错误= -1,套接字在SOAP服务器建立后立即关闭
2.如果我们在soap_free_stream()之后调用xxx.destory(),那么soap_accept()将报告错误的文件描述符错误而不再工作(已解决)
有人知道上述现象的原因是什么?怎么解决?
我们的代码非常接近示例,除了一些更改,请参阅下面的部分.
//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(&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->soap_stream_fault(std::cerr);
}
else if (abc->dispatch() == SOAP_NO_METHOD)
{
soap_copy_stream(&uvw, abc);
uww.state = SOAP_COPY;
if (uvw.dispatch() == SOAP_NO_METHOD)
{
soap_copy_stream(&xyz, &uvw);
xyz.state = SOAP_COPY;
if (xyz.dispatch())
{
soap_send_fault(&xyz); // send fault to client
xyz.soap_stream_fault(std::cerr);
}
soap_free_stream(&xyz); // free the copy
xyz.destroy();
}
else
{
soap_send_fault(&uvw); // send fault to client
uvw.soap_stream_fault(std::cerr);
}
soap_free_stream(&uvw); // free the copy
uvw.destroy();
}
else if (abc->error)
{
abc->soap_stream_fault(std::cerr);
}
else
{
}
abc->destroy();
delete abc;
abc = NULL;
}
Run Code Online (Sandbox Code Playgroud)
最后我找到了为什么有些连接建立后立即被服务器关闭的原因。
这不是 gSOAP 服务器的错,而是因为所有连接都来自同一台计算机,这些客户端被设置为重用地址和端口重用导致了此问题。
| 归档时间: |
|
| 查看次数: |
313 次 |
| 最近记录: |