[英]Boost Asio - Server doesn't receive message
I'm developing a 3d (first/third person) game and I'm trying to make it multiplayer using TCP sockets. 我正在开发一个3D(第一/第三人称)游戏,我正在尝试使用TCP套接字制作多人游戏。 I'm using the boost asio library for this, and I'm in a little over my head.
我正在使用boost asio库,我有点过头了。 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.
我已经使用了关于boost asio doc页面的教程和示例,他们编译/运行/工作得很好,我只是对一切如何工作有点困惑。
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.
更具体地说,客户端似乎成功发送了消息,服务器似乎永远不会收到消息。
Client code: 客户代码:
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;
}
Server code: 服务器代码:
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));
}
It does not look like your io_service
has any work to do when you invoke run()
. 当你调用
run()
时,它看起来不像你的io_service
有任何工作要做。
bool ServerFramework::Initialize()
{
mIOService.run(); // <-- you don't check the return value here
StartAccept();
return true;
}
it will return the number of handlers executed , I suspect it is zero. 它会返回执行的处理程序数 ,我怀疑它是零。 Try invoking it after
async_accept()
尝试在
async_accept()
之后调用它
bool ServerFramework::Initialize()
{
StartAccept();
mIOService.run();
return true;
}
Though, it isn't entirely obvious by your limited code snippets where you invoke ServerFramework::Initialize()
. 但是,您调用
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()
. 如果没有其他样板文件,例如
main()
,您的当前代码将无法编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.