<legend id='Vr3FP'><style id='Vr3FP'><dir id='Vr3FP'><q id='Vr3FP'></q></dir></style></legend>
    • <bdo id='Vr3FP'></bdo><ul id='Vr3FP'></ul>
    <i id='Vr3FP'><tr id='Vr3FP'><dt id='Vr3FP'><q id='Vr3FP'><span id='Vr3FP'><b id='Vr3FP'><form id='Vr3FP'><ins id='Vr3FP'></ins><ul id='Vr3FP'></ul><sub id='Vr3FP'></sub></form><legend id='Vr3FP'></legend><bdo id='Vr3FP'><pre id='Vr3FP'><center id='Vr3FP'></center></pre></bdo></b><th id='Vr3FP'></th></span></q></dt></tr></i><div id='Vr3FP'><tfoot id='Vr3FP'></tfoot><dl id='Vr3FP'><fieldset id='Vr3FP'></fieldset></dl></div>
  • <small id='Vr3FP'></small><noframes id='Vr3FP'>

      <tfoot id='Vr3FP'></tfoot>

        BOOST ASIO - 如何编写控制台服务器

        时间:2023-07-20
          <tbody id='8bNgr'></tbody>
        • <bdo id='8bNgr'></bdo><ul id='8bNgr'></ul>

          <tfoot id='8bNgr'></tfoot>
            1. <small id='8bNgr'></small><noframes id='8bNgr'>

            2. <legend id='8bNgr'><style id='8bNgr'><dir id='8bNgr'><q id='8bNgr'></q></dir></style></legend>

                  <i id='8bNgr'><tr id='8bNgr'><dt id='8bNgr'><q id='8bNgr'><span id='8bNgr'><b id='8bNgr'><form id='8bNgr'><ins id='8bNgr'></ins><ul id='8bNgr'></ul><sub id='8bNgr'></sub></form><legend id='8bNgr'></legend><bdo id='8bNgr'><pre id='8bNgr'><center id='8bNgr'></center></pre></bdo></b><th id='8bNgr'></th></span></q></dt></tr></i><div id='8bNgr'><tfoot id='8bNgr'></tfoot><dl id='8bNgr'><fieldset id='8bNgr'></fieldset></dl></div>
                  本文介绍了BOOST ASIO - 如何编写控制台服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我必须编写异步 TCP 服务器.TCP Server 必须由控制台管理(例如:删除客户端,显示所有已连接客户端的列表等.)

                  I have to write asynchronous TCP Sever. TCP Server have to be managed by console (for eg: remove client, show list of all connected client, etcc..)

                  问题是:我如何附加(或编写)可以调用上述功能的控制台.这个控制台必须是客户端吗?我应该将这个控制台客户端作为一个单独的线程运行吗?

                  The problem is: How can I attach (or write) console, which can calls above functionalities. This console have to be a client? Should I run this console client as a sepearate thread?

                  我阅读了很多教程,但找不到解决问题的方法.

                  I have read a lot of tutorials and I couldn`t find a solution to my problem.

                  服务器TCP代码

                  class ServerTCP
                  {
                  public:
                     ServerTCP(boost::asio::io_service& A_ioService, unsigned short A_uPortNumber = 13)
                        : m_tcpAcceptor(A_ioService, tcp::endpoint(tcp::v4(), A_uPortNumber)), m_ioService (A_ioService)
                     {
                        start();
                     }
                  private:
                  
                     void start()
                     {
                        ClientSessionPtr spClient(new ClientSession(m_tcpAcceptor.io_service(), m_connectedClients));
                  
                        m_tcpAcceptor.async_accept(spClient->getSocket(), 
                                                   boost::bind(&ServerTCP::handleAccept, this, spClient, 
                                                   boost::asio::placeholders::error));
                  
                     }
                     void handleAccept(ClientSessionPtr A_spNewClient,  const boost::system::error_code& A_nError)
                     {
                        if ( !A_nError )
                        {
                           A_spNewClient->start();
                           start();
                        }
                     }
                  
                  
                  
                     boost::asio::io_service& m_ioService;
                     tcp::acceptor            m_tcpAcceptor;
                     Clients                  m_connectedClients;
                  };
                  

                  主要功能:

                     try
                     {
                        boost::asio::io_service ioService;
                  
                        ServerTCP server(ioService);
                        ioService.run();  
                     }
                     catch (std::exception& e)
                     {
                        std::cerr << "Exception: " << e.what() << "
                  ";
                     }
                  

                  你好山姆.谢谢你的回复.你能不能给我看一段代码或一些与这个问题相关的例子的链接?可能,我没有正确理解......单线程服务器......"

                  Hello Sam. Thanks for reply. Could you be so kind and show me a some piece of code or some links to examples involve with this problem ? Propably, I don`t understand correctly "... single threaded server ..."

                  事实上,在我想管理服务器操作的控制台"中,我需要像下面这样的 smt:

                  In Fact in "console" where I want to manage server operations, I need smt like below:

                  main()
                  
                  cout << "Options: q - close server, s - show clients";
                  while(1)
                  {
                    char key = _getch();
                    switch( key )
                    {
                        case 'q':
                           closeServer();
                        break
                        case 's':
                           showClients();
                        break
                    } 
                  }
                  

                  推荐答案

                  问题是:我如何附加(或write) 控制台,可以在上面调用功能.这个控制台必须成为客户?我应该运行这个控制台吗客户端作为一个单独的线程?

                  The problem is: How can I attach (or write) console, which can calls above functionalities. This console have to be a client? Should I run this console client as a sepearate thread?

                  您不需要单独的线程,使用 posix::stream_descriptor 和 assign STDIN_FILENO 给它.使用 async_read 并处理读取处理程序中的请求.

                  You don't need a separate thread, use a posix::stream_descriptor and assign STDIN_FILENO to it. Use async_read and handle the requests in the read handlers.

                  #include <boost/asio.hpp>
                  
                  #include <boost/bind.hpp>
                  #include <boost/enable_shared_from_this.hpp>
                  #include <boost/shared_ptr.hpp>
                  
                  #include <iostream>
                  
                  using namespace boost::asio;
                  
                  class Input : public boost::enable_shared_from_this<Input>
                  {
                  public:
                      typedef boost::shared_ptr<Input> Ptr;
                  
                  public:
                      static void create(
                              io_service& io_service
                              )
                      {
                          Ptr input(
                                  new Input( io_service )
                                  );
                          input->read();
                      }
                  
                  private:
                      explicit Input(
                              io_service& io_service
                           ) :
                          _input( io_service )
                      {
                          _input.assign( STDIN_FILENO );
                      }
                  
                      void read()
                      {
                          async_read(
                                  _input,
                                  boost::asio::buffer( &_command, sizeof(_command) ),
                                  boost::bind(
                                      &Input::read_handler,
                                      shared_from_this(),
                                      placeholders::error,
                                      placeholders::bytes_transferred
                                      )
                                  );
                      }
                  
                      void read_handler(
                              const boost::system::error_code& error,
                              size_t bytes_transferred
                              )
                      {
                          if ( error ) {
                              std::cerr << "read error: " << boost::system::system_error(error).what() << std::endl;
                              return;
                          }
                  
                          if ( _command != '
                  ' ) {
                              std::cout << "command: " << _command << std::endl;
                          }
                  
                          this->read();
                      }
                  
                  private:
                      posix::stream_descriptor _input;
                      char _command;
                  };
                  
                  int
                  main()
                  {
                      io_service io_service;
                      Input::create( io_service );
                      io_service.run();
                  }
                  

                  这篇关于BOOST ASIO - 如何编写控制台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用boost预处理器生成访问器? 下一篇:使用 boost 精神解析 int 或 double (longest_d)

                  相关文章

                  <legend id='jwgVh'><style id='jwgVh'><dir id='jwgVh'><q id='jwgVh'></q></dir></style></legend>
                  <i id='jwgVh'><tr id='jwgVh'><dt id='jwgVh'><q id='jwgVh'><span id='jwgVh'><b id='jwgVh'><form id='jwgVh'><ins id='jwgVh'></ins><ul id='jwgVh'></ul><sub id='jwgVh'></sub></form><legend id='jwgVh'></legend><bdo id='jwgVh'><pre id='jwgVh'><center id='jwgVh'></center></pre></bdo></b><th id='jwgVh'></th></span></q></dt></tr></i><div id='jwgVh'><tfoot id='jwgVh'></tfoot><dl id='jwgVh'><fieldset id='jwgVh'></fieldset></dl></div>

                      • <bdo id='jwgVh'></bdo><ul id='jwgVh'></ul>
                    1. <tfoot id='jwgVh'></tfoot>

                      <small id='jwgVh'></small><noframes id='jwgVh'>