Boost Asio-服务器未收到消息 [英] Boost Asio - Server doesn't receive message

查看:94
本文介绍了Boost Asio-服务器未收到消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发3d(第一人称/第三人称)游戏,并且尝试使用TCP套接字使其成为多人游戏.我正在为此使用boost asio库,但有点不知所措.我在boost asio文档页面上玩了一些教程和示例,它们的编译/运行/工作都很好,我对一切的工作方式有些困惑.

I'm developing a 3d (first/third person) game and I'm trying to make it multiplayer using TCP sockets. I'm using the boost asio library for this, and I'm in a little over my head. I've played with the tutorials and examples a bit on the boost asio doc page and they compiled/ran/worked just fine, I'm just a little confused as to how everything works.

现在,我只是想让服务器接受来自客户端的消息,并在接收到消息后打印该消息.当我执行下面的代码时(它可以编译/链接/运行良好),什么也没发生.更具体地说,客户端似乎已成功发送了邮件,而服务器似乎从未收到过该邮件.

Right now I'm just trying to make the server accept messages from the client, and print the message after receiving it. When I execute the code below (it compiles/links/runs fine), nothing happens. More specifically, the client appears to successfully send the message, and the server never seems to receive the message.

客户代码:

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

推荐答案

在调用run()时,您的io_service似乎没有任何工作要做.

It does not look like your io_service has any work to do when you invoke run().

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

    return true;
}

它将返回执行的处理程序数量,我怀疑它是零.尝试在async_accept()

it will return the number of handlers executed, I suspect it is zero. Try invoking it after async_accept()

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

    return true;
}

但是,通过调用ServerFramework::Initialize()的有限代码段并不能完全看出这一点.我建议使用简短,自包含的正确示例编辑您的问题,我们可以毫不费力地进行编译.没有其他样板内容,例如main(),您的当前代码将无法编译.

Though, it isn't entirely obvious by your limited code snippets where you invoke ServerFramework::Initialize(). I suggest editing your question with a short, self contained, correct example that we can compile with little to no effort. Your current code will not compile without additional boilerplate stuff, like main().

这篇关于Boost Asio-服务器未收到消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆