<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 - 如何编写控制台服务器

        BOOST ASIO - How to write console server(BOOST ASIO - 如何编写控制台服务器)
          <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 - 如何编写控制台服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to limit the number of running instances in C++(C++中如何限制运行实例的数量)
                  Using boost::asio::async_read with stdin?(将 boost::asio::async_read 与 stdin 一起使用?)
                  How to find out what dependencies (i.e other Boost libraries) a particular Boost library requires?(如何找出特定 Boost 库需要哪些依赖项(即其他 Boost 库)?)
                  What#39;s the purpose of a leading quot;::quot; in a C++ method call(引导“::的目的是什么?在 C++ 方法调用中)
                  Boost Spirit x3: parse into structs(Boost Spirit x3:解析为结构体)
                  How boost auto-linking makes choice?(boost自动链接如何做出选择?)
                  <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>

                          <tbody id='jwgVh'></tbody>

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

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