简体   繁体   中英

boost ASIO server segmentation fault

I created a server using 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. 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.

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 .

Just reorder the declarations inside the class. Surprisingly clang did not give any warning for this.

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();
    }


};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM