首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >boost::asio无限循环

boost::asio无限循环
EN

Stack Overflow用户
提问于 2012-06-13 20:34:30
回答 1查看 3.3K关注 0票数 0

这是我的boost::asio服务器

代码语言:javascript
复制
class Server: public boost::enable_shared_from_this<Server>, private boost::noncopyable{
  private:
    boost::asio::ip::tcp::acceptor _acceptor;
    boost::asio::ip::tcp::socket   _socket;
  public:
    explicit Server(boost::asio::io_service& ios, boost::asio::ip::tcp::endpoint& endpoint):_acceptor(ios, endpoint), _socket(ios){

    }
    void start(){
       accept();
    }
    void accept(){
       std::cout << "accepting " << std::endl;;
      _acceptor.async_accept(_socket, boost::bind(&Server::handler, this, boost::asio::placeholders::error));
    }
    void handler(const boost::system::error_code &ec){
       const std::string message = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!";
       if(!ec){
         boost::asio::async_write(_socket, boost::asio::buffer(message), boost::bind(&Server::write_handler, this));
       }else{
         std::cout << ec << std::endl;
       }
       accept();
    }
    void write_handler(){

    }
    boost::asio::ip::tcp::socket& socket(){
      return _socket;
    }
};


int main(){
  boost::asio::io_service ios;
  const unsigned int port = 5050;
  boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port);

  Server server(ios, endpoint);
  server.start();

  ios.run();
  return 0;
}

它第一次以“Hallo World”作为响应;然后它只是在accept <--> handler循环中不断循环,并且不写入欢迎消息。ec打印

代码语言:javascript
复制
asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
asio.misc:1
accepting 
......

永远不会停止

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-13 21:06:13

无限循环是使用_socket的结果。第一个async_accept()之所以有效,是因为没有使用_socket。但是,_socket永远不会关闭,因此使用_socketasync_accept()的其他调用将失败。async_accept()的对等参数期望套接字未被使用,因为它将使用套接字进行新连接。这可以通过以下两种方法解决:

  • 为每个连接分配一个新套接字。考虑通过boost::shared_ptr管理套接字。这允许服务器处理多个并发connections.
  • Closing write_handler中的_socket,然后调用accept()。这将服务器限制为一次只能连接一个。

另外,要小心使用async_write()。底层缓冲区内存的所有权由调用方保留,调用方必须保证它在调用处理程序之前保持有效。在这种情况下,在调用write_handler()之前,message将从堆栈中弹出。在messageconst的情况下,考虑将其设置为static以保证其持续时间。

在将对象传递给bind调用的实例时,请使用shared_from_this()而不是this。否则,this指向的实例可能会被删除,因为只有在使用shared_from_this()时才会正确地进行引用计数。

最后,在打印boost::system::error_code时,使用error_code.message()方法获得更有意义的消息。在无限循环的情况下,它将打印“已打开”。

下面是一次只支持一个连接的修改后的handler()write_handler()代码:

代码语言:javascript
复制
void accept(){
   std::cout << "accepting " << std::endl;;
  _acceptor.async_accept(_socket, boost::bind(&Server::handler, shared_from_this(), boost::asio::placeholders::error));
}
void handler(const boost::system::error_code &ec){
   // Guarantee message will remain valid throughout the duration of async_write.
   static const std::string message = "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nHello, world!";
   if(!ec){
     // write_handler will accept the next connection once it is done with the socket.
     boost::asio::async_write(_socket, boost::asio::buffer(message), boost::bind(&Server::write_handler, shared_from_this()));
   }else{
     std::cout << ec.message() << std::endl;
     // Try accepting on error.
     accept();
   }
}
void write_handler(){
   _socket.close();
   // Now that the socket is closed, new connectiosn can be accepted.
   accept();
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11014918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档