Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

c++ - BOOST ASIO - How to write console 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.

ServerTCP code

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;
};

Main function:

   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 ..."

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
  } 
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...