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

2021-12-24 00:00:00 tcp c++ boost 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();
}

相关文章