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

标签 c++ boost tcp boost-asio

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

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

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

ServerTCP代码

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() << "\n";
   }

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

事实上,在我想管理服务器操作的“控制台”中,我需要如下所示的 smt:

main()

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

最佳答案

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_descriptorassign STDIN_FILENO 到它。使用 async_read 并在读取处理程序中处理请求。

#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 != '\n' ) {
            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();
}

关于c++ - BOOST ASIO - 如何编写控制台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5210796/

相关文章:

c++ - 使用 GNU 科学库 (GSL) 的 C/C++ 代码为 GNUPlot 提供了不同的结果 - 可能存在浮点不稳定性?

c++ - 为什么使用 for_each 是错误的?

c++ - 具有 undefined reference 和重定位的简单程序被截断以适应错误

c++ - std::vector<int> 到 std::vector<enum>

c++ - Boost上下文实现

silverlight TCP隧道/桥接

c++ - 使用英特尔编译器 12 和 Visual Studio 2010 编译 boost 1_46_1 时出现问题

c++ - 在我的函数中删除对 boost::bind 的需要

tcp - 即使端口上没有任何运行,绑定(bind)地址已在使用中的 golang 错误

amazon-web-services - AWS 中的 TCP 中继功能(如 ngrok)