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.
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
As documented,
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: