Boost Asio - 服务器无法接收消息

时间:2012-02-19 22:32:52

标签: c++ sockets boost tcp boost-asio

我正在开发一个3D(第一/第三人称)游戏,我正在尝试使用TCP套接字进行多人游戏。我正在使用boost asio库,我有点过头了。我已经使用了关于boost asio doc页面的教程和示例,他们编译/运行/工作得很好,我只是对一切如何工作有点困惑。

现在我只是想让服务器接受来自客户端的消息,并在收到消息后打印消息。当我执行下面的代码(它编译/链接/运行正常)时,没有任何反应。更具体地说,客户端似乎成功发送了消息,服务器似乎永远不会收到消息。

客户代码:

ClientFramework::ClientFramework() :
    mResolver(mIOService)
{
}

bool ClientFramework::Initialize()
{
    try
    {
        tcp::resolver::query query("localhost", "daytime");
        tcp::resolver::iterator it = mResolver.resolve(query);
        tcp::socket socket(mIOService);
        boost::asio::connect(socket, it);

        std::string s = "hello world";
        boost::system::error_code e;
        socket.write_some(boost::asio::buffer(s.c_str(), s.size()), e);

        if (e)
        {
            throw boost::system::system_error(e);
        }
    } catch (std::exception& e)
    {
        gLog << LOG_ERROR << e.what() << "\n";
    }

    return true;
}

服务器代码:

ServerFramework::ServerFramework() :
    mAcceptor(mIOService, tcp::endpoint(tcp::v4(), 13))
{
}

bool ServerFramework::Initialize()
{
    mIOService.run();
    StartAccept();

    return true;
}

void ServerFramework::StartAccept()
{
    Connection::ptr conn =
        Connection::create(mAcceptor.get_io_service());
    mAcceptor.async_accept(conn->Socket(),
        boost::bind(&ServerFramework::HandleAccept, this, conn,
            boost::asio::placeholders::error));
}

void ServerFramework::HandleAccept(Connection::ptr conn,
    const boost::system::error_code& error)
{
    if (!error)
    {
        conn->Initialize();
    }

    StartAccept();
}

Connection::ptr Connection::create(boost::asio::io_service& io_service)
{
    return ptr(new Connection(io_service));
}

tcp::socket& Connection::Socket()
{
    return mSocket;
}

void Connection::Initialize()
{
    boost::asio::async_read(mSocket, boost::asio::buffer(buf, BUFFER_SIZE),
        boost::bind(&Connection::handle_read, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
}

Connection::Connection(boost::asio::io_service& io_service) :
    mSocket(io_service)
{
}

void Connection::handle_read(const boost::system::error_code& e, size_t size)
{
    std::string s(buf, size);
    gLog << LOG_INFO << s << "\n";

    boost::asio::async_read(mSocket, boost::asio::buffer(buf, BUFFER_SIZE),
        boost::bind(&Connection::handle_read, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
}

1 个答案:

答案 0 :(得分:4)

当您调用io_service时,run()看起来没有任何工作要做。

bool ServerFramework::Initialize()
{
    mIOService.run(); // <-- you don't check the return value here
    StartAccept();

    return true;
}

它会返回number of handlers executed,我怀疑它是零。尝试在async_accept()

之后调用它
bool ServerFramework::Initialize()
{
    StartAccept();
    mIOService.run();

    return true;
}

尽管如此,您调用ServerFramework::Initialize()的有限代码片段并不完全明显。我建议使用short, self contained, correct example编辑您的问题,我们可以轻松编译。如果没有其他样板文件,您的当前代码将无法编译,例如main()