[英]boost ASIO server segmentation fault
I created a server using Boost ASIO. 我使用Boost ASIO创建了服务器。 It builds fine but as soon as I run it, it gives segmentation fault.
它可以很好地构建,但是一旦我运行它,它就会产生分段错误。 Can't really figure out this behaviour.
无法真正弄清楚这种行为。
Also, I read that this may be due to me not initialising the io_service object explicitly. 另外,我读到这可能是由于我没有显式初始化io_service对象。 If, that's the case then how do I modify this code so that I don't have to pass io_service object from outside the class.
如果是这样的话,那么我该如何修改此代码,这样我就不必从类外部传递io_service对象。
Below is my code: 下面是我的代码:
#include <iostream>
#include <string>
#include <memory>
#include <array>
#include <boost/asio.hpp>
using namespace boost::asio;
//Connection Class
class Connection : public std::enable_shared_from_this<Connection>{
ip::tcp::socket m_socket;
std::array<char, 2056> m_acceptMessage;
std::string m_acceptMessageWrapper;
std::string m_buffer;
public:
Connection(io_service& ioService): m_socket(ioService) { }
virtual ~Connection() { }
static std::shared_ptr<Connection> create(io_service& ioService){
return std::shared_ptr<Connection>(new Connection(ioService));
}
std::string& receiveMessage() {
size_t len = boost::asio::read(m_socket, boost::asio::buffer(m_acceptMessage));
m_acceptMessageWrapper = std::string(m_acceptMessage.begin(), m_acceptMessage.begin() + len);
return m_acceptMessageWrapper;
}
void sendMessage(const std::string& message) {
boost::asio::write(m_socket, boost::asio::buffer(message));
}
ip::tcp::socket& getSocket(){
return m_socket;
}
};
//Server Class
class Server {
ip::tcp::acceptor m_acceptor;
io_service m_ioService ;
public:
Server(int port):
m_acceptor(m_ioService, ip::tcp::endpoint(ip::tcp::v4(), port)){ }
virtual ~Server() { }
std::shared_ptr<Connection> createConnection(){
std::shared_ptr<Connection> newConnection = Connection::create(m_ioService);
m_acceptor.accept(newConnection->getSocket());
return newConnection;
}
void runService() {
m_ioService.run();
}
};
int main(int argc, char* argv[]) {
Server s(5000);
auto c1 = s.createConnection();
//do soething
s.runService();
return 0;
}
You are facing initialisation order issues. 您面临初始化订单问题。 In your class
Server
, you have declared m_acceptor
before m_ioService
and using the uninitialized io_service
object to construct the acceptor
. 在你的类
Server
,你已经宣布m_acceptor
之前m_ioService
和使用未初始化的io_service
对象构造acceptor
。
Just reorder the declarations inside the class. 只需对类中的声明重新排序。 Surprisingly
clang
did not give any warning for this. 令人惊讶的是,
clang
没有为此提供任何警告。
class Server {
io_service m_ioService ;
ip::tcp::acceptor m_acceptor;
public:
Server(int port):
m_acceptor(m_ioService, ip::tcp::endpoint(ip::tcp::v4(), port)){ }
virtual ~Server() { }
std::shared_ptr<Connection> createConnection(){
std::shared_ptr<Connection> newConnection = Connection::create(m_ioService);
m_acceptor.accept(newConnection->getSocket());
return newConnection;
}
void runService() {
m_ioService.run();
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.