gpt4 book ai didi

c++ - boost async_read

转载 作者:行者123 更新时间:2023-11-30 03:04:39 25 4
gpt4 key购买 nike

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

Waiting for a new connection

Connection accepted

terminate called throwing an exceptionAbort trap: 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;
}

最佳答案

你应该包装你的 main()try {...} catch (std::exception& e) { cout << e.what(); } 中发挥作用 block 。

关于c++ - boost async_read,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8484789/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com