提升 async_read

发布于 2024-12-21 09:24:46 字数 993 浏览 0 评论 0原文

我正在尝试创建一个 TCP 服务器,其中 Start() 方法会阻塞,直到接受连接,然后开始一系列异步读取。我有以下代码,当我使用 telnet 连接时,我得到以下输出:

等待新连接

连接已接受

终止调用抛出异常中止陷阱:6

这是代码:

void SocketReadThread::Start()
{
    bzero(m_headerBuffer, HEADER_LEN);
    m_running = true;

    asio::io_service ios;
    asio::ip::tcp::acceptor acp (ios,
        boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), GUI_PORT));

    asio::ip::tcp::socket sock(ios);
    std::cout << "Waiting for a new connection" << std::endl;

    acp.accept(sock); 
    std::cout << "Connection accepted" << std::endl;

    asio::async_read(sock, asio::buffer(m_headerBuffer, HEADER_LEN),
        boost::bind(&SocketReadThread::handleReadHeader, shared_from_this(),
        asio::placeholders::error));

    ios.run();
}

void SocketReadThread::handleReadHeader(const system::error_code& error)
{
    std::cout << "Read two bytes!" << std::endl;
}

I'm trying to create a TCP server where the Start() method blocks until a connection is accepted, and then begins a series of asynchronous reads. I have the following code, and when I connect using telnet I get this output:

Waiting for a new connection

Connection accepted

terminate called throwing an exceptionAbort trap: 6

Here is the code:

void SocketReadThread::Start()
{
    bzero(m_headerBuffer, HEADER_LEN);
    m_running = true;

    asio::io_service ios;
    asio::ip::tcp::acceptor acp (ios,
        boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), GUI_PORT));

    asio::ip::tcp::socket sock(ios);
    std::cout << "Waiting for a new connection" << std::endl;

    acp.accept(sock); 
    std::cout << "Connection accepted" << std::endl;

    asio::async_read(sock, asio::buffer(m_headerBuffer, HEADER_LEN),
        boost::bind(&SocketReadThread::handleReadHeader, shared_from_this(),
        asio::placeholders::error));

    ios.run();
}

void SocketReadThread::handleReadHeader(const system::error_code& error)
{
    std::cout << "Read two bytes!" << std::endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不甘平庸 2024-12-28 09:24:46

您应该将 main() 函数包装在 try {...} catch (std::exception& e) { cout << e.what(); } 块。

You should wrap your main() function in try {...} catch (std::exception& e) { cout << e.what(); } block.

骄傲 2024-12-28 09:24:46

通过错误地声明 ReadHandler,您可能正在对堆栈做一些可怕的(而且很棒的)事情。即使忽略某些参数,签名也必须是:

void handler (
  const boost::system::error_code& error, // Result of operation.

  std::size_t bytes_transferred           // Number of bytes copied into the
                                          // buffers. If an error occurred,
                                          // this will be the  number of
                                          // bytes successfully transferred
                                          // prior to the error.
);

You're probably doing something scary (and awesome) to the stack by declaring your ReadHandler incorrectly. Even if you ignore some parameters, the signature must be:

void handler (
  const boost::system::error_code& error, // Result of operation.

  std::size_t bytes_transferred           // Number of bytes copied into the
                                          // buffers. If an error occurred,
                                          // this will be the  number of
                                          // bytes successfully transferred
                                          // prior to the error.
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文