How do I create a Boost.Asio Server that can handl

2020-02-09 23:22发布

I can create a simple TCP server that can respond to one client, but I don't know how to create a server that can handle multiple clients at once. I have referred to examples like TCP daytime async server, but it just sends the data to client. What I need to create is keep alive the connection as long as client exists. Both Client and Server will communicate in Json. Consider one case where client will give {"hello":"Client"} and server should respond {"Hello":"Server"}, and say another {"message":"How are you?"} and its response {"response" : "Fine"}. I need to handle multiple clients at once. I read Chat server documentation, but it is too hard to comprehend. Can someone give basic flow of how to start the code using Boost.Asio? Thanks.

Below is the code given:

#include <ctime>
#include <iostream>
#include <string>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>
#include <json/reader.h>
#include <json/writer.h>

Json::Value GetRootFromJson(std::string json)
{
    Json::Value root;
    Json::Reader reader;
    bool success = reader.parse(json, root);
    return root;
}

std::string GetJsonFromRoot(Json::Value root)
{
    Json::FastWriter writer;
    std::string json = writer.write(root);
    return json;

}

using boost::asio::ip::tcp;

std::string make_daytime_string()
{
  using namespace std; // For time_t, time and ctime;
  time_t now = time(0);
  return ctime(&now);
}

class tcp_connection
  : public boost::enable_shared_from_this<tcp_connection>
{
public:
  typedef boost::shared_ptr<tcp_connection> pointer;

  static pointer create(boost::asio::io_service& io_service)
  {
    return pointer(new tcp_connection(io_service));
  }

  tcp::socket& socket()
  {
    return socket_;
  }

  void start()
  {
    //message_ = make_daytime_string();

    //Read from client, make json and send appropriate response
    boost::asio::async_read(socket_, boost::asio::buffer(message_),
    boost::bind(&tcp_connection::handle_read, shared_from_this(),
        boost::asio::placeholders::error,
        boost::asio::placeholders::bytes_transferred));
    std::string messageP(message_);
    std::cout << messageP << std::endl;
    Json::Value root = GetRootFromJson(messageP);
    std::string isHello = root["hello"].asString();
    std::string isMessage = root["message"].asString();
    if(!isHello.empty())
    {
        std::string messageTemp = "{\"Hello\":\"Server\"}";
        boost::asio::async_write(socket_, boost::asio::buffer(messageTemp),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
    }

    if(!isMessage.empty())
    {
        std::string messageTemp = "{\"response\":\"Fine\"}";
        boost::asio::async_write(socket_, boost::asio::buffer(messageTemp),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
    }



    /*
    boost::asio::async_write(socket_, boost::asio::buffer(message_),
        boost::bind(&tcp_connection::handle_write, shared_from_this(),
          boost::asio::placeholders::error,
          boost::asio::placeholders::bytes_transferred));
    */
  }

private:
  tcp_connection(boost::asio::io_service& io_service)
    : socket_(io_service)
  {
  }

  void handle_write(const boost::system::error_code& /*error*/,
      size_t /*bytes_transferred*/)
  {
  }
  void handle_read(const boost::system::error_code& /*error*/,
      size_t /*bytes_transferred*/)
  {
      std::cout << "Handle Read of connection\n";
  }

  tcp::socket socket_;
  std::string message_;
};

class tcp_server
{
public:
  tcp_server(boost::asio::io_service& io_service)
    : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1936))
  {
    start_accept();
  }

private:
  void start_accept()
  {
    tcp_connection::pointer new_connection =
      tcp_connection::create(acceptor_.get_io_service());

    acceptor_.async_accept(new_connection->socket(),
        boost::bind(&tcp_server::handle_accept, this, new_connection,
          boost::asio::placeholders::error));
  }

  void handle_accept(tcp_connection::pointer new_connection,
      const boost::system::error_code& error)
  {
    if (!error)
    {
        std::cout << "A client connected" << std::endl;
      new_connection->start();
    }

    start_accept();
  }

  tcp::acceptor acceptor_;
};

int main()
{
  try
  {
    boost::asio::io_service io_service;
    tcp_server server(io_service);
    io_service.run();
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

Running this gives me:

Error 2 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const boost::asio::const_buffer' (or there is no acceptable conversion) C:\boost_1_55_0_dyn\boost\asio\detailconsuming_buffers.hpp 175

Edit: I have added a very simple client code, that will send "{\"Hello\":\"Client\"}" to the server and expects the output. But the server code, given by sehe could not enter into handle_read

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

using boost::asio::ip::tcp;

int main(int argc, char* argv[])
{
  try
  {

    boost::asio::io_service io_service;

    tcp::resolver resolver(io_service);
    tcp::resolver::query query("localhost", "1936");
    tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);

    tcp::socket socket(io_service);
    boost::asio::connect(socket, endpoint_iterator);

    std::string message = "{\"hello\":\"Client\"}";
    std::cout << "Sending the message: " << message << std::endl;
    socket.send(boost::asio::buffer(message));
    std::cout << "Sent the message: " << message << std::endl;
    boost::asio::streambuf buf;
    size_t len;
    std::string messageReceived;
    boost::asio::streambuf::mutable_buffers_type mbuf = buf.prepare(512);
    std::cout << "Now receiving message\n";
    std::string messageServer;
    try
    {
        boost::asio::streambuf buf;
        size_t len;
        boost::asio::streambuf::mutable_buffers_type mbuf = buf.prepare(512);
        do {
            len = socket.receive(mbuf);
            std::cout << len << std::endl;
            std::string str(boost::asio::buffers_begin(mbuf), boost::asio::buffers_begin(mbuf) + len);
            messageServer = messageServer + str;
        } while (len>=512); 
    }
    catch (boost::system::system_error err)
    {
        std::cout << err.code() << " " << err.what();
    }

    std::cout << messageServer << std::endl;
  }
  catch (std::exception& e)
  {
    std::cerr << e.what() << std::endl;
  }

  return 0;
}

Is this the correct way to implement the server? Thank you.

2条回答
家丑人穷心不美
2楼-- · 2020-02-10 00:11

The includes you need. Some maybe are unnecessary:

boost/asio.hpp, boost/thread.hpp, boost/asio/io_service.hpp

boost/asio/spawn.hpp, boost/asio/write.hpp, boost/asio/buffer.hpp

boost/asio/ip/tcp.hpp, iostream, stdlib.h, array, string

vector, string.h, stdio.h, process.h, iterator

using namespace boost::asio;
using namespace boost::asio::ip;

io_service ioservice;

tcp::endpoint sim_endpoint{ tcp::v4(), 4066 };              //{which connectiontype, portnumber}
tcp::acceptor sim_acceptor{ ioservice, sim_endpoint };
std::vector<tcp::socket> sim_sockets;

static int iErgebnis;
int iSocket = 0;


void do_write(int a)                                        //gets the postion of the socket in the vector
{
    int iWSchleife = 1;                                     //to stay connected with putty or something
    static char chData[32000];
    std::string sBuf = "Received!\r\n";

    while (iWSchleife > 0)          
    {
        boost::system::error_code error;
        memset(chData, 0, sizeof(chData));

        iErgebnis = sim_sockets[a].read_some(boost::asio::buffer(chData), error);           //recv wie bei winsock. simulator empfängt
        iWSchleife = iErgebnis;                                                             //if iErgebnis is bigger then 0 it will stay in the loop. iErgebniss is always >0 when data is received

        if (iErgebnis > 0) {
            printf("%d Zeichen empf.vom Client : \n%s\n\n", iErgebnis, chData);
            write(sim_sockets[a], boost::asio::buffer(sBuf), error);
        }
        else {
            boost::system::error_code ec;
            sim_sockets[a].shutdown(boost::asio::ip::tcp::socket::shutdown_send, ec);       //close the socket when no data
            if (ec)
            {
                printf("studown error");                                                    // An error occurred.
            }
        }
    }
}

void do_accept(yield_context yield)
{
    while (1)                                                   //endless loop to accept limitless clients
    {
        sim_sockets.emplace_back(ioservice);                    //look to the link below for more info
        sim_acceptor.async_accept(sim_sockets.back(), yield);   //waits here to accept an client

        boost::thread dosome(do_write, iSocket);                //when accepts starts the thread do_write and passes the parameter iSocket
        iSocket++;                                              //to know the position of the socket in the vector

    }
}

int main()
{
    sim_acceptor.listen();
    spawn(ioservice, do_accept);            //here you can learn more about Coroutines https://theboostcpplibraries.com/boost.coroutine
    ioservice.run();                        //from here you jump to do:accept
    getchar(); 
}
查看更多
时光不老,我们不散
3楼-- · 2020-02-10 00:18

As documented,

boost::asio::async_read(socket_, boost::asio::buffer(message_),

takes a streambuf.

Further more it is an asynchronous operations, so it makes zero sense to access message_ after posting the read operation, and before the completion handler (handle_read) is called back.

The following at least compiles for me:

#include <ctime>
#include <iostream>
#include <string>

#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include <json/reader.h>
#include <json/writer.h>

namespace {
    Json::Value to_json(std::string json)
    {
        Json::Value root;
        Json::Reader reader;
        /*bool success =*/ reader.parse(json, root);
        return root;
    }

    std::string to_string(Json::Value root) // unused TODO FIXME
    {
        Json::FastWriter writer;
        std::string json = writer.write(root);
        return json;
    }
}

using boost::asio::ip::tcp;

class tcp_connection : public boost::enable_shared_from_this<tcp_connection>
{
  public:
    typedef boost::shared_ptr<tcp_connection> pointer;

    static pointer create(boost::asio::io_service& io_service)
    {
        return pointer(new tcp_connection(io_service));
    }

    tcp::socket& socket()
    {
        return socket_;
    }

    void start()
    {
        //Read from client, make json and send appropriate response
        boost::asio::async_read(socket_, message_,
                boost::bind(&tcp_connection::handle_read, shared_from_this(),
                    boost::asio::placeholders::error,
                    boost::asio::placeholders::bytes_transferred));
    }

  private:
    tcp_connection(boost::asio::io_service& io_service)
        : socket_(io_service)
    {
    }

    void handle_write(const boost::system::error_code& /*error*/,
            size_t /*bytes_transferred*/)
    {
    }

    void handle_read(const boost::system::error_code& error, size_t bytes_transferred)
    {
        std::cout << "Handle Read of connection\n";

        if (error && error != boost::asio::error::eof) {
            std::cout << "Error: " << error.message() << "\n";
            return;
        }

        std::string messageP;
        {
            std::stringstream ss;
            ss << &message_;
            ss.flush();
            messageP = ss.str();
        }

        std::cout << messageP << std::endl;

        Json::Value root      = to_json(messageP);
        std::string isHello   = root["hello"].asString();
        std::string isMessage = root["message"].asString();
        if(!isHello.empty())
        {
            std::string messageTemp = "{\"Hello\":\"Server\"}";
            boost::asio::async_write(socket_, boost::asio::buffer(messageTemp),
                    boost::bind(&tcp_connection::handle_write, shared_from_this(),
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
        }

        if(!isMessage.empty())
        {
            std::string messageTemp = "{\"response\":\"Fine\"}";
            boost::asio::async_write(socket_, boost::asio::buffer(messageTemp),
                    boost::bind(&tcp_connection::handle_write, shared_from_this(),
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
        }
    }

    tcp::socket socket_;
    boost::asio::streambuf message_;
};

class tcp_server
{
  public:
    tcp_server(boost::asio::io_service& io_service)
        : acceptor_(io_service, tcp::endpoint(tcp::v4(), 1936))
    {
        start_accept();
    }

  private:
    void start_accept()
    {
        tcp_connection::pointer new_connection =
            tcp_connection::create(acceptor_.get_io_service());

      acceptor_.async_accept(new_connection->socket(),
          boost::bind(&tcp_server::handle_accept, this, new_connection,
            boost::asio::placeholders::error));
    }

    void handle_accept(tcp_connection::pointer new_connection, const boost::system::error_code& error)
    {
        if (!error)
        {
            std::cout << "A client connected" << std::endl;
            new_connection->start();
        }

        start_accept();
    }

    tcp::acceptor acceptor_;
};

int main()
{
    try
    {
        boost::asio::io_service io_service;
        tcp_server server(io_service);
        io_service.run();
    }
    catch (std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}
查看更多
登录 后发表回答