简体   繁体   English

boost::asio 服务器,功能简单

[英]boost::asio server with simple functions

Guys I really need your help.伙计们,我真的需要你们的帮助。 I'm learning boost::asio and I have 2 problems that I can't deal for days...我正在学习boost::asio并且我有两个问题几天都无法解决......

Here is an example of a simple echo server done by myself:下面是我自己做的一个简单的回显服务器的例子:

int main(
{
    // crate a server, binding it and listening connections

    // acceptor server;

    //socket client

    server.async_accept(client, boost::bind(accept_handle, _1, &server, &client));

    io_service.run();

    return 0;
}

void accept_handle(const boost::system::error_code& eCode, boost::asio::ip::tcp::acceptor* server, boost::asio::ip::tcp::socket* client)
{
    char data[43];
    client->async_read_some(boost::asio::buffer(data, 20), boost::bind(read_handle, _1, _2, server, client));
}

void read_handle(const boost::system::error_code& eCode, size_t bytes)
{
    char data_buf[20] = "hello";
    client->async_write_some(boost::buufer(data, 5), boost::bind(write_handle, _1, _2, server, client)); 
}

void write_accept(const boost::system::error_code& eCode, size_t bytes)
{
    boost::asio::ip::tcp::socket newConnection(server->get_ioservice)); // taking he io_service of the server

    server->async_accept(newConnection, boost::bind(accept_handle, _1, server, client));
}

The problem is the server accept one client and it does not accept other pending client.. where am i doing wrong here问题是服务器接受一个客户端,它不接受其他待处理的客户端..我在这里做错了什么

NOTE : I wrote this code in notepad so sorry for syntax errors if there are any.注意:我在记事本中写了这段代码,如果有语法错误,请见谅。

Thanks for your help in advance!!!提前感谢您的帮助!!!

The code can only accept one connection because it is not calling async_accept in the accept_handle function.该代码只能接受一个连接,因为它没有accept_handle函数中调用async_accept

The code may also have a problem with object lifetimes: it would be wise to use shared pointers to manage the clients see: Boost async_* functions and shared_ptr's .代码也可能有对象生命周期的问题:使用共享指针来管理clients是明智的,请参阅: Boost async_* 函数和 shared_ptr's

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM