创建tcp :: acceptor

时间:2015-10-04 10:10:42

标签: c++ boost-asio

我有一个刚刚开始段错误的简单程序。 main()函数如下所示:

int main()
{
    try
    {
        std::string peerRequestPort;
        std::cout << "enter port listening: ";
        std::cin >> peerRequestPort;

        asio::io_service io_service;

        network::PeerManager peerManager(network::Peer({"0,0,0,0",peerRequestPort}));

        network::PeerRequestServer peerRequestServer(io_service,peerRequestPort);
        peerRequestServer.Start(peerManager);

我在asio::ip::tcp::acceptor构造函数中创建PeerRequestServer时遇到了段错误。 PeerManager的构造函数不进行任何数据操作或任何操作,它只存储提供的Peer结构。 PeerRequestServer构造函数也没有做任何事情,只是使用提供的asio::ip::tcp::acceptor启动asio::io_service&

调用堆栈是

#0 0041C77D asio::detail::service_registry::keys_match(key1=..., key2=...) 
#1 ??   0x0041cbd2 in asio::detail::service_registry::do_use_service (this=0xa631a0, key=..., factory=0x41cef8 <asio::detail::service_registry::create<asio::socket_acceptor_service<asio::ip::tcp> >(asio::io_service&)>) 
#2 0041CA6A asio::detail::service_registry::use_service<asio::socket_acceptor_service<asio::ip::tcp> >(this=0xa631a0) 
#3 004116CB asio::use_service<asio::socket_acceptor_service<asio::ip::tcp> >(ios=...) 
#4 00412A2B asio::basic_io_object<asio::socket_acceptor_service<asio::ip::tcp>, true>::basic_io_object(this=0x28fcec, io_service=...)
#5 00413079 asio::basic_socket_acceptor<asio::ip::tcp, asio::socket_acceptor_service<asio::ip::tcp> >::basic_socket_acceptor(this=0x28fcec, io_service=..., endpoint=..., reuse_addr=true) 
#6 00409513 network::PeerRequestServer::PeerRequestServer(this=0x28fcec, ioService=..., portListen=...) 
#7 00401B24 _fu38___ZSt3cin()

这是PeerRequestServer的构造函数:

PeerRequestServer::PeerRequestServer(asio::io_service& ioService, std::string portListen)
: m_acceptor(ioService, tcp::endpoint(tcp::v4(),std::stoi(portListen)))
{
    std::clog << __func__ << std::endl;
}

PeerManager s:

PeerManager::PeerManager(Peer self)
: m_self(self)
{
}

所以我无法看到从主要开始到PeerRequestServer构造函数的位置,我有可能导致段错误...

2 个答案:

答案 0 :(得分:2)

IP不应该是0.0.0.0而不是0,0,0,0吗?

答案 1 :(得分:2)

你正在做其他错误而你没有表现出来。

当然拼写0,0,0,0值得怀疑。但是你展示的代码工作得很好:

<强> Live On Coliru

  

这就是您应该创建SSCCEhttps://stackoverflow.com/help/mcve

的原因
#include <boost/asio.hpp>
#include <iostream>

namespace asio = boost::asio;
using asio::ip::tcp;

namespace network {
    struct Peer {
        Peer(tcp::resolver::query) {}
    };

    struct PeerManager {
        PeerManager(Peer self) : m_self(self) {}
        Peer m_self;
    };

    struct PeerRequestServer {
        PeerRequestServer(asio::io_service &ioService, std::string portListen)
            : m_acceptor(ioService, tcp::endpoint(tcp::v4(), std::stoi(portListen))) {
                std::cout << __PRETTY_FUNCTION__ << std::endl;
            }

        void Start(PeerManager&) {
            // TODO 
        }

        tcp::acceptor m_acceptor;
    };
}

int
main() {
    try {
        std::string peerRequestPort;
        std::cout << "enter port listening: ";
        std::cin >> peerRequestPort;

        asio::io_service io_service;

        network::PeerManager peerManager(network::Peer({ "0,0,0,0", peerRequestPort }));

        network::PeerRequestServer peerRequestServer(io_service, peerRequestPort);
        peerRequestServer.Start(peerManager);
    } catch (std::exception const &e) {
        std::cerr << "Exception: " << e.what() << "\n";
    }
}