I'm trying to use serial_port of asio(standlone) to get data from a device. But I can't read even a byte.
I post a screenshoot of the example usage found from documentation website as below :
enter image description here
enter image description here
https://think-async.com/Asio/asio-1.24.0/doc/asio/reference/basic_serial_port/async_read_some.html
There are 3 question I have:
What is the specific type of the first parameter of async_read_some? std::vector<uint8_t>, for example?
How to bind a class member function as the callback/handler?
Is it right that we just make sure io_context.run() run on a background thread and call async_read_some just once?
I'll appreciate it if you guys could help me check out my code or give some advice.
class NmeaSource {
public:
explicit NmeaSource(std::shared_ptr<asio::io_context> io, const nlohmann::json& settings);
void parse();
void handle(const asio::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes read.
);
private:
void open();
std::string m_port;
int m_baudrate;
std::shared_ptr<asio::io_context> m_io;
std::shared_ptr<asio::serial_port> m_serial;
unsigned char m_readBuffer[1024];
};
NmeaSource::NmeaSource(std::shared_ptr<asio::io_context> io, const nlohmann::json& settings)
:m_io(io)
{
try {
setPort(settings.at("port"));
setBaudrate(settings.at("baudrate"));
//LOG("");
std::cout << "port: " << m_port << ", baudrate: " << m_baudrate << std::endl;
}
catch (nlohmann::json::basic_json::type_error& e1) {
std::cout << "type is wrong " << e1.what() << std::endl;
throw e1;
}
catch (nlohmann::json::basic_json::out_of_range& e2) {
std::cout << "key is not found " << e2.what() << std::endl;
throw e2;
}
catch (...)
{
std::cout << "unknown error"<< std::endl;
exit(-1);
}
open();
}
void NmeaSource::open()
{
m_serial = std::make_shared<asio::serial_port>(*m_io);
asio::error_code ec;
m_serial->open(m_port, ec);
if (!ec) {
asio::serial_port_base::baud_rate baudrate(m_baudrate);
m_serial->set_option(baudrate);
std::cout << "successfully" << std::endl;
}
else {
std::cout << "failed " << ec.message() <<std::endl;
}
}
void NmeaSource::handle(const asio::error_code& error, // Result of operation.
std::size_t bytes_transferred // Number of bytes read.
)
{
if (!error) {
std::cout << bytes_transferred << " bytes read!" << std::endl;
}
else {
std::cout << error.message() << std::endl;
}
}
void NmeaSource::parse()
{
m_serial->async_read_some(
asio::buffer(m_readBuffer, 1024),
std::bind(&NmeaSource::handle, this,
std::placeholders::_1,
std::placeholders::_2)
);
}
int main()
{
auto io = std::make_shared<asio::io_context>();
//std::thread t(std::bind(static_cast<size_t(asio::io_service::*)()>(&asio::io_service::run), io.get()));
std::thread t([&io]() {io->run(); });
NmeaSource rtk(io, nlohmann::json{ {"port", "COM3"}, {"baudrate", 115200} });
rtk.parse();
for (;;)
{
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}
The output
I have a server/client application which works for a write from client to a read at server.
Inside the startHandlig function in the server code, if I comment async_connect_1 and the return after it, then it works fine which involves sync write function.
I added async_connect_1 function inside Service() class to asynchronously read from the socket.
This function is called when a client connects to the server and this function returns immediately.
I expect the callback function corresponding to async_read to be called, but that is not happening...
I'm stuck at this since a long time.. Appreciate help on this...
Server Code
#include <boost/asio.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/tuple/tuple.hpp>
#include <thread>
#include <atomic>
#include <memory>
#include <iostream>
#include "../stocks.hpp"
using namespace boost;
class Service {
public:
Service(){}
void StartHandligClient(
std::shared_ptr<asio::ip::tcp::socket> sock) {
read_async_1(sock);
return;
std::thread th(([this, sock]() {
HandleClient(sock);
}));
std::cout << "Detached \n";
th.detach();
}
private:
void read_async_1(std::shared_ptr<asio::ip::tcp::socket> sock)
{
if(!(*sock.get()).is_open())
{
std::cout << getpid() << " : Socket closed in sync_read \n" << std::flush;
return ;
}
std::cout << "haha_1\n" << std::flush;
boost::asio::async_read( (*sock.get()), boost::asio::buffer(inbound_header_),
[this](boost::system::error_code ec,
size_t bytesRead)
{
std::cout << "haha_2\n" << std::flush;
if (!ec)
{
int headerBytesReceived = bytesRead;
std::cout << "\n\n headerBytesReceived : " << headerBytesReceived << "\n" << std::flush ;
// this->async_read(sock);
}
else
{
// Terminate connection ?
if(ec == boost::asio::error::eof)
{
std::cout << getpid() << " : ** sync_read : Connection lost : boost::asio::error::eof ** \n";
}
std::cout << "Error occured in sync_read! Error code = " << ec.value() << ". Message: " << ec.message() << "\n" << std::flush;
return ;
}
return ;
}
);
std::cout << getpid() << " : final return from async_read \n" << std::flush;
return ;
}
void HandleClient(std::shared_ptr<asio::ip::tcp::socket> sock) {
while(1)
{
try {
// asio::streambuf request;
// asio::read_until(*sock.get(), request, '\n');
int headerBytesReceived = asio::read( *sock.get(), boost::asio::buffer(inbound_header_) );
std::cout << "headerBytesReceived : " << headerBytesReceived << "\n" << std::flush;
// Determine the length of the serialized data.
std::istringstream is(std::string(inbound_header_, header_length));
std::cout << "is : " << is.str() << ", inbound_header_ : " << inbound_header_ << "\n";
std::size_t inbound_data_size = 0;
if (!(is >> std::hex >> inbound_data_size))
{
// Header doesn't seem to be valid. Inform the caller.
// boost::system::error_code error(boost::asio::error::invalid_argument);
// boost::get<0>(handler)(error);
std::cout << "RET-1 \n";
return;
}
std::cout << "inbound_data_size : " << inbound_data_size << "\n" << std::flush;
// Start an asynchronous call to receive the data.
inbound_data_.resize(inbound_data_size);
std::cout << "inbound_data_.size() : " << inbound_data_.size() << "\n" << std::flush;
int bytesReceived = asio::read( *sock.get(), boost::asio::buffer(inbound_data_) );
std::string archive_data(&inbound_data_[0], inbound_data_.size());
std::istringstream archive_stream(archive_data);
boost::archive::text_iarchive archive(archive_stream);
archive >> stocks_;
std::cout << "bytesReceived : " << bytesReceived << " , stocks_.size() : " << stocks_.size() << "\n";
// Print out the data that was received.
for (std::size_t i = 0; i < stocks_.size(); ++i)
{
std::cout << "Stock number " << i << "\n";
std::cout << " code: " << stocks_[i].code << "\n";
std::cout << " name: " << stocks_[i].name << "\n";
std::cout << " open_price: " << stocks_[i].open_price << "\n";
std::cout << " high_price: " << stocks_[i].high_price << "\n";
std::cout << " low_price: " << stocks_[i].low_price << "\n";
std::cout << " last_price: " << stocks_[i].last_price << "\n";
std::cout << " buy_price: " << stocks_[i].buy_price << "\n";
std::cout << " buy_quantity: " << stocks_[i].buy_quantity << "\n";
std::cout << " sell_price: " << stocks_[i].sell_price << "\n";
std::cout << " sell_quantity: " << stocks_[i].sell_quantity << "\n";
}
}
catch (system::system_error &e)
{
boost::system::error_code ec = e.code();
if(ec == boost::asio::error::eof)
{
std::cout << "EOF Error \n";
}
std::cout << "Server Error occured! Error code = "
<< e.code() << ". Message: "
<< e.what() << "\n";
break;
}
}
// Clean-up.
delete this;
}
/// The size of a fixed length header.
enum { header_length = 8 };
/// Holds an outbound header.
std::string outbound_header_;
/// Holds the outbound data.
std::string outbound_data_;
/// Holds an inbound header.
char inbound_header_[header_length];
/// Holds the inbound data.
std::vector<char> inbound_data_;
std::vector<stock> stocks_;
};
class Acceptor {
public:
Acceptor(asio::io_service& ios, unsigned short port_num) :
m_ios(ios),
m_acceptor(m_ios,
asio::ip::tcp::endpoint(
asio::ip::address_v4::any(),
port_num))
{
m_acceptor.listen();
}
void Accept() {
std::cout << "Server Accept() \n" << std::flush;
std::shared_ptr<asio::ip::tcp::socket>
sock(new asio::ip::tcp::socket(m_ios));
m_acceptor.accept(*sock.get());
(new Service)->StartHandligClient(sock);
}
private:
asio::io_service& m_ios;
asio::ip::tcp::acceptor m_acceptor;
};
class Server {
public:
Server() : m_stop(false) {}
void Start(unsigned short port_num) {
m_thread.reset(new std::thread([this, port_num]() {
Run(port_num);
}));
}
void Stop() {
m_stop.store(true);
m_thread->join();
}
private:
void Run(unsigned short port_num) {
Acceptor acc(m_ios, port_num);
while (!m_stop.load()) {
std::cout << "Server accept\n" << std::flush;
acc.Accept();
}
}
std::unique_ptr<std::thread> m_thread;
std::atomic<bool> m_stop;
asio::io_service m_ios;
};
int main()
{
unsigned short port_num = 3333;
try {
Server srv;
srv.Start(port_num);
std::this_thread::sleep_for(std::chrono::seconds(100));
std::cout << "Stopping server \n";
srv.Stop();
}
catch (system::system_error &e) {
std::cout << "Error occured! Error code = "
<< e.code() << ". Message: "
<< e.what();
}
return 0;
}
Client Code
#include <boost/asio.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include "../stocks.hpp"
using namespace boost;
class SyncTCPClient {
public:
SyncTCPClient(const std::string& raw_ip_address,
unsigned short port_num) :
m_ep(asio::ip::address::from_string(raw_ip_address),
port_num),
m_sock(m_ios) {
m_sock.open(m_ep.protocol());
}
void connect() {
m_sock.connect(m_ep);
}
void close() {
m_sock.shutdown(
boost::asio::ip::tcp::socket::shutdown_both);
m_sock.close();
}
std::string emulateLongComputationOp(
unsigned int duration_sec) {
std::string request = "EMULATE_LONG_COMP_OP "
+ std::to_string(duration_sec)
+ "\n";
sendRequest(request);
return receiveResponse();
};
private:
void sendRequest(const std::string& request) {
std::vector<stock> stocks_;
// Create the data to be sent to each client.
stock s;
s.code = "ABC";
s.name = "A Big Company";
s.open_price = 4.56;
s.high_price = 5.12;
s.low_price = 4.33;
s.last_price = 4.98;
s.buy_price = 4.96;
s.buy_quantity = 1000;
s.sell_price = 4.99;
s.sell_quantity = 2000;
stocks_.push_back(s);
// Serialize the data first so we know how large it is.
std::ostringstream archive_stream;
boost::archive::text_oarchive archive(archive_stream);
archive << stocks_;
outbound_data_ = archive_stream.str();
std::cout << "outbound_data_ : " << outbound_data_ << "\n" << std::flush;
std::cout << "outbound_data_.size() : " << outbound_data_.size() << "\n" << std::flush;
// Format the header.
std::ostringstream header_stream;
header_stream << std::setw(header_length) << std::hex << outbound_data_.size();
std::cout << "header_stream.str() : " << header_stream.str() << "\n" << std::flush;
std::cout << "header_stream.str().size() : " << header_stream.str().size() << "\n" << std::flush;
if (!header_stream || header_stream.str().size() != header_length)
{
// Something went wrong, inform the caller.
// boost::system::error_code error(boost::asio::error::invalid_argument);
// socket_.get_io_service().post(boost::bind(handler, error));
return;
}
outbound_header_ = header_stream.str();
std::cout << "outbound_header_ : " << outbound_header_ << "\n" << std::flush;
// Write the serialized data to the socket. We use "gather-write" to send
// both the header and the data in a single write operation.
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(outbound_header_));
buffers.push_back(boost::asio::buffer(outbound_data_));
std::size_t sizeSent = asio::write(m_sock, buffers);
std::cout << "sizeSent : " << sizeSent << "\n" << std::flush;
}
std::string receiveResponse() {
std::string response;
/*
asio::streambuf buf;
asio::read_until(m_sock, buf, '\n');
std::istream input(&buf);
std::getline(input, response);
*/
return response;
}
private:
asio::io_service m_ios;
asio::ip::tcp::endpoint m_ep;
asio::ip::tcp::socket m_sock;
enum { header_length = 8 };
std::string outbound_data_;
std::string outbound_header_;
};
int main()
{
const std::string raw_ip_address = "127.0.0.1";
const unsigned short port_num = 3333;
try {
SyncTCPClient client(raw_ip_address, port_num);
// Sync connect.
client.connect();
sleep(1);
std::cout << "Sending request to the server... "
<< std::endl;
std::string response = client.emulateLongComputationOp(10);
std::cout << "Response received: " << response << std::endl;
sleep(100);
std::cout << "\n\n Closing client connection \n\n";
// Close the connection and free resources.
client.close();
}
catch (system::system_error &e) {
std::cout << "Client Error occured! Error code = " << e.code()
<< ". Message: " << e.what();
return e.code().value();
}
return 0;
}
Included File (stocks.hpp)
#ifndef _STOCKS_HPP_
#define _STOCKS_HPP_
struct stock
{
std::string code;
std::string name;
double open_price;
double high_price;
double low_price;
double last_price;
double buy_price;
int buy_quantity;
double sell_price;
int sell_quantity;
template <typename Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & code;
ar & name;
ar & open_price;
ar & high_price;
ar & low_price;
ar & last_price;
ar & buy_price;
ar & buy_quantity;
ar & sell_price;
ar & sell_quantity;
}
};
#endif
You have written Error code = 125. Message: Operation canceled as comment in previous response, i think that socket may be closed before async operation will be done.
What is lifetime of your socket ?
[1] socket is created in Accept method
std::shared_ptr<asio::ip::tcp::socket>
sock(new asio::ip::tcp::socket(m_ios)); // ref count +1
//...
(new Service)->StartHandligClient(sock); // this function returns immediately
// so socket's ref count -1
[2] in StartHandligClient()
sock is passed by value, so ref count of socket +1, but
void StartHandligClient(
std::shared_ptr<asio::ip::tcp::socket> sock) { // +1 ref count
read_async_1(sock); // this function returns immediately
return; // -1 ref count of socket
}
[3] in read_async_1 socket is passed by value, +1 on ref count of socket, but this function returns immediately, when function ends, ref count is decreased and socket object is deleted.
You created lambda object to execute asynchronus operation, but socket object may be closed before doing it.
You did apparently use a asio::io_service, but you forgot to run it.
m_ios.run();
Run the io_context object's event processing loop.
Fix this and your handler[s] will be called.
You can either create a thread for this, or call it in your main function in your 'main-thread'.
std::thread([this]() { m_ios.run(); } );
Note: Don't forget to stop (1) it later and join the thread (2) if you created one.
I've been trying to use boost in order to asynchronously read from a serial_port until it puts incoming data into a buffer that I have specified. However, when I call:
boost::asio::async_read(*port_,
*readBuf_,
boost::bind(&Serial::async_read_handler,
this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
it calls async_read_handler immediately, which triggers the code inside my read_handler.
void Serial::async_read_handler(const boost::system::error_code &e,
std::size_t bytes_read)
{
namespace basio = boost::asio;
if(!e)
{
std::cout << e.message() << std::endl;
std::cout << bytes_read << std::endl;
if(bytes_read <= 0)
{
std::string read = *basio::buffer_cast<std::string*>(*readBuf_);
std::cout << read << std::endl;
basio::async_read(*port_,
*readBuf_,
boost::bind(&Serial::async_read_handler,
this,
basio::placeholders::error,
basio::placeholders::bytes_transferred));
}
else
{
std::string read = *basio::buffer_cast<std::string*>(*readBuf_);
std::cout << read << std::endl;
std::cout << "Bytes read: " << bytes_read << std::endl;
}
}
else
{
std::cerr << e.message() << std::endl;
}
}
I first call a thread to run async_read in my main
int main()
{
boost::asio::io_service io;
Serial::Serial serial(PORT, &io);
if(!serial.is_open())
{
serial.open(PORT);
}
serial.async_read();
io.run();
return 0;
}
I want the async_read call to wait until it has read data from the buffer to call the handler. How can I do this? Thanks.
I want to transfer some files via tcp over lan, and so I wrote the following code for my TX-Part:
void send_data(char * filename, char * dest)
{
try
{
boost::asio::io_service io_service;
char dest_t = *dest;
std::string adr = ip_adr_display[0] + ':' + boost::lexical_cast<std::string>(PORTNUM_TCP_IN);
std::cout << "Adress is: " << adr << " and file is: " << filename << '\n';
if(debugmode)
debug_global << adr << '\n';
std::string file = filename;
async_tcp_client client(io_service, adr, file);
io_service.run();
}
catch(std::exception& e)
{
};
};
and RX-Part:
void rec_data(void)
{
try
{
std::cout << "Receiving data...\n";
async_tcp_server *recv_file_tcp_server = new async_tcp_server(PORTNUM_TCP_IN);
if(debugmode)
debug_global << "Receiving...\n";
delete recv_file_tcp_server;
}
catch(std::exception &e)
{
};
};
with the following server and client code:
using boost::asio::ip::tcp;
class async_tcp_client
{
public:
async_tcp_client(boost::asio::io_service& io_service, const std::string& server, const std::string& path):resolver_(io_service), socket_(io_service)
{
size_t pos = server.find(':');
if(pos==std::string::npos)
return;
std::string port_string = server.substr(pos+1);
std::string server_ip_or_host = server.substr(0,pos);
source_file.open(path.c_str(), std::ios_base::binary|std::ios_base::ate);
if(!source_file)
{
std::cout << "Failed to open " << path << std::endl;
return;
}
size_t file_size = source_file.tellg();
source_file.seekg(0);
std::ostream request_stream(&request_);
request_stream << path << "\n" << file_size << "\n\n";
std::cout << "Request size: " << request_.size() << std::endl;
tcp::resolver::query query(server_ip_or_host, port_string);
resolver_.async_resolve(query, boost::bind(&async_tcp_client::handle_resolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
};
private:
void handle_resolve(const boost::system::error_code & err, tcp::resolver::iterator endpoint_iterator)
{
if(!err)
{
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
}
else
{
std::cout << "Error: " << err.message() << '\n';
}
};
void handle_connect(const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
{
if(!err)
{
boost::asio::async_write(socket_, request_, boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
}
else if(endpoint_iterator != tcp::resolver::iterator())
{
socket_.close();
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
}
else
{
std::cout << "Error: " << err.message() << '\n';
};
}
void handle_write_file(const boost::system::error_code& err)
{
if(!err)
{
if(source_file.eof() == false)
{
source_file.read(buf.c_array(), (std::streamsize)buf.size());
if(source_file.gcount()<= 0)
{
std::cout << "read file error" << std::endl;
return;
};
std::cout << "Send " << source_file.gcount() << "bytes, total: " << source_file.tellg() << " bytes.\n";
boost::asio::async_write(socket_, boost::asio::buffer(buf.c_array(), source_file.gcount()),boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
if(err)
{
std::cout << "Send error: " << err << std::endl;
return;
}
}
else
return;
}
else
{
std::cout << "Error: " << err.message() << "\n";
}
};
tcp::resolver resolver_;
tcp::socket socket_;
boost::array<char, 1024> buf;
boost::asio::streambuf request_;
std::ifstream source_file;
};
class async_tcp_connection: public boost::enable_shared_from_this<async_tcp_connection>
{
public:
async_tcp_connection(boost::asio::io_service& io_service):socket_(io_service), file_size(0){}
void start()
{
if(debugmode)
debug_global << __FUNCTION__ << std::endl;
async_read_until(socket_, request_buf, "\n\n", boost::bind(&async_tcp_connection::handle_read_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
boost::asio::ip::tcp::socket& socket(){return socket_;}
private:
boost::asio::streambuf request_buf;
size_t file_size;
std::ofstream output_file;
boost::asio::ip::tcp::socket socket_;
boost::array<char, 40960> buf;
void handle_read_request(const boost::system::error_code& err, std::size_t bytes_transferred)
{
if(err)
{
return handle_error(__FUNCTION__, err);
}
if(debugmode)
debug_global << __FUNCTION__ << "(" << bytes_transferred << ")" <<", in_avail = " << request_buf.in_avail() << ", size = " << request_buf.size() << ", max_size = " << request_buf.max_size() << ".\n";
std::istream request_stream(&request_buf);
std::string file_path;
request_stream >> file_path;
request_stream >> file_size;
request_stream.read(buf.c_array(), 2);
if(debugmode)
debug_global << file_path << " size is " << file_size << ", tellg = " << request_stream.tellg() << std::endl;
size_t pos = file_path.find_last_of('\\');
if(pos!= std::string::npos)
file_path = file_path.substr(pos+1);
output_file.open(file_path.c_str(), std::ios_base::binary);
if(!output_file)
{
if(debugmode)
debug_global << "Failed to open: " << file_path << std::endl;
return;
}
do{
request_stream.read(buf.c_array(), (std::streamsize)buf.size());
if(debugmode)
debug_global << __FUNCTION__ << " write " << request_stream.gcount() << " bytes.\n";
output_file.write(buf.c_array(), request_stream.gcount());
}while(request_stream.gcount() > 0);
async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()),boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void handle_read_file_content(const boost::system::error_code& err, std::size_t bytes_transferred)
{
if (bytes_transferred>0)
{
output_file.write(buf.c_array(), (std::streamsize)bytes_transferred);
if(debugmode)
debug_global << __FUNCTION__ << " recv " << output_file.tellp() << " bytes."<< std::endl;
if (output_file.tellp()>=(std::streamsize)file_size)
{
return;
}
}
if (err)
{
return handle_error(__FUNCTION__, err);
}
async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()), boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void handle_error(const std::string& function_name, const boost::system::error_code& err)
{
if(debugmode)
debug_global << __FUNCTION__ << " in " << function_name <<" due to " << err <<" " << err.message()<< std::endl;
}
};
class async_tcp_server : private boost::noncopyable
{
public:
typedef boost::shared_ptr<async_tcp_connection> ptr_async_tcp_connection;
async_tcp_server(unsigned short port):acceptor_(io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port), true)
{
ptr_async_tcp_connection new_connection_(new async_tcp_connection(io_service_));
acceptor_.async_accept(new_connection_->socket(), boost::bind(&async_tcp_server::handle_accept, this,new_connection_, boost::asio::placeholders::error));
io_service_.run();
}
void handle_accept(ptr_async_tcp_connection current_connection, const boost::system::error_code& e)
{
if(debugmode)
debug_global << __FUNCTION__ << " " << e << ", " << e.message()<<std::endl;
if (!e)
{
current_connection->start();
//ptr_async_tcp_connection new_connection_(new async_tcp_connection(io_service_));
//acceptor_.async_accept(new_connection_->socket(),
// boost::bind(&async_tcp_server::handle_accept, this,new_connection_,
// boost::asio::placeholders::error));
}
}
~async_tcp_server()
{
io_service_.stop();
}
private:
boost::asio::io_service io_service_;
boost::asio::ip::tcp::acceptor acceptor_;
};
If I want to transmit a file, I have to enter the absolute path (why?), if I enter the relative path (e.g. "Image.jpg"), I get the error message "failed to open Image.jpg".
After successfull calling the function, I get the following output:
Adress is: <ip>:<port> and file is: <full file path>
Request size: 91
Send 1024 bytes, total: 1024 bytes
Send 1024 bytes, total: 2048 bytes
etc..
Send 1024 bytes, total: 20480 bytes
Send 406 bytes, total: -1 bytes (Why?)
At the receiving side, I get no received data. Why? I do not understand why my code is not working...
Thank you very much!
UPDATE In my answer I casually said
I added postfix .received to the output file name to prevent overwriting the source.
I just realized that this is likely your problem:
If you use your code with the receiver on the same machine as the sender, you will overwrite the source file while you are still sending it... OOPS.
So, I fixed up the code just I could run it.
Here's the test main:
int main()
{
boost::thread_group g;
g.create_thread(rec_data); // get the receiver running
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
g.create_thread([] { send_data("test.cpp"); });
g.join_all();
}
I added postfix .received to the output file name to prevent overwriting the source.
When running this, it appears to work reasonably well:
g++ -std=c++11 -Wall -pedantic -pthread test.cpp -lboost_system -lboost_thread
./a.out
md5sum test.cpp test.cpp.received
We get the output
0dc16e7f0dc23cb9fce100d825852621 test.cpp.received
0dc16e7f0dc23cb9fce100d825852621 test.cpp
I've also tested it with a png and with a 93Mb executable.
Full code (also on Coliru, although Coliru doesn't allow network connections):
#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <iostream>
#include <fstream>
#include <boost/enable_shared_from_this.hpp>
using boost::asio::ip::tcp;
static bool debugmode = true;
static boost::mutex debug_mutex;
static std::ostream debug_global(std::clog.rdbuf());
class async_tcp_client
{
public:
async_tcp_client(boost::asio::io_service& io_service, const std::string& server, const std::string& path)
: resolver_(io_service), socket_(io_service)
{
size_t pos = server.find(':');
if(pos==std::string::npos)
{
return;
}
std::string port_string = server.substr(pos+1);
std::string server_ip_or_host = server.substr(0,pos);
source_file.open(path, std::ios_base::binary|std::ios_base::ate);
if(!source_file)
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << __LINE__ << "Failed to open " << path << std::endl;
return;
}
size_t file_size = source_file.tellg();
source_file.seekg(0);
std::ostream request_stream(&request_);
request_stream << path << "\n" << file_size << "\n\n";
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "Request size: " << request_.size() << std::endl;
}
tcp::resolver::query query(server_ip_or_host, port_string);
resolver_.async_resolve(query, boost::bind(&async_tcp_client::handle_resolve, this, boost::asio::placeholders::error, boost::asio::placeholders::iterator));
};
private:
void handle_resolve(const boost::system::error_code & err, tcp::resolver::iterator endpoint_iterator)
{
if(!err)
{
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
}
else
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "Error: " << err.message() << '\n';
}
};
void handle_connect(const boost::system::error_code &err, tcp::resolver::iterator endpoint_iterator)
{
if(!err)
{
boost::asio::async_write(socket_, request_, boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
}
else if(endpoint_iterator != tcp::resolver::iterator())
{
socket_.close();
tcp::endpoint endpoint = *endpoint_iterator;
socket_.async_connect(endpoint, boost::bind(&async_tcp_client::handle_connect, this, boost::asio::placeholders::error, ++endpoint_iterator));
}
else
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "Error: " << err.message() << '\n';
};
}
void handle_write_file(const boost::system::error_code& err)
{
if(!err)
{
if(source_file)
//if(source_file.eof() == false)
{
source_file.read(buf.c_array(), (std::streamsize)buf.size());
if(source_file.gcount()<= 0)
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "read file error" << std::endl;
return;
};
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "Send " << source_file.gcount() << "bytes, total: " << source_file.tellg() << " bytes.\n";
}
boost::asio::async_write(socket_, boost::asio::buffer(buf.c_array(), source_file.gcount()),boost::bind(&async_tcp_client::handle_write_file, this, boost::asio::placeholders::error));
}
else
{
return;
}
}
else
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "Error: " << err.message() << "\n";
}
};
tcp::resolver resolver_;
tcp::socket socket_;
boost::array<char, 1024> buf;
boost::asio::streambuf request_;
std::ifstream source_file;
};
class async_tcp_connection: public boost::enable_shared_from_this<async_tcp_connection>
{
public:
async_tcp_connection(boost::asio::io_service& io_service)
: socket_(io_service), file_size(0)
{
}
void start()
{
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << __FUNCTION__ << std::endl;
}
async_read_until(socket_, request_buf, "\n\n", boost::bind(&async_tcp_connection::handle_read_request, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
boost::asio::ip::tcp::socket& socket()
{
return socket_;
}
private:
boost::asio::streambuf request_buf;
std::ofstream output_file;
boost::asio::ip::tcp::socket socket_;
size_t file_size;
boost::array<char, 40960> buf;
void handle_read_request(const boost::system::error_code& err, std::size_t bytes_transferred)
{
if(err)
{
return handle_error(__FUNCTION__, err);
}
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << __FUNCTION__ << "(" << bytes_transferred << ")" <<", in_avail = " << request_buf.in_avail() << ", size = " << request_buf.size() << ", max_size = " << request_buf.max_size() << ".\n";
}
std::istream request_stream(&request_buf);
std::string file_path;
request_stream >> file_path;
request_stream >> file_size;
request_stream.read(buf.c_array(), 2);
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << file_path << " size is " << file_size << ", tellg = " << request_stream.tellg() << std::endl;
}
size_t pos = file_path.find_last_of('\\');
if(pos!= std::string::npos)
{
file_path = file_path.substr(pos+1);
}
output_file.open(file_path + ".received", std::ios_base::binary);
if(!output_file)
{
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << __LINE__ << "Failed to open: " << file_path << std::endl;
}
return;
}
do
{
request_stream.read(buf.c_array(), (std::streamsize)buf.size());
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << __FUNCTION__ << " write " << request_stream.gcount() << " bytes.\n";
}
output_file.write(buf.c_array(), request_stream.gcount());
}
while(request_stream.gcount() > 0);
async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()),boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void handle_read_file_content(const boost::system::error_code& err, std::size_t bytes_transferred)
{
if (bytes_transferred>0)
{
output_file.write(buf.c_array(), (std::streamsize)bytes_transferred);
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << __FUNCTION__ << " recv " << output_file.tellp() << " bytes."<< std::endl;
}
if (output_file.tellp()>=(std::streamsize)file_size)
{
return;
}
}
if (err)
{
return handle_error(__FUNCTION__, err);
}
async_read(socket_, boost::asio::buffer(buf.c_array(), buf.size()), boost::bind(&async_tcp_connection::handle_read_file_content, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void handle_error(const std::string& function_name, const boost::system::error_code& err)
{
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << __FUNCTION__ << " in " << function_name <<" due to " << err <<" " << err.message()<< std::endl;
}
}
};
class async_tcp_server : private boost::noncopyable
{
public:
typedef boost::shared_ptr<async_tcp_connection> ptr_async_tcp_connection;
async_tcp_server(unsigned short port):acceptor_(io_service_, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port), true)
{
ptr_async_tcp_connection new_connection_(new async_tcp_connection(io_service_));
acceptor_.async_accept(new_connection_->socket(), boost::bind(&async_tcp_server::handle_accept, this,new_connection_, boost::asio::placeholders::error));
io_service_.run();
}
void handle_accept(ptr_async_tcp_connection current_connection, const boost::system::error_code& e)
{
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << __FUNCTION__ << " " << e << ", " << e.message()<<std::endl;
}
if (!e)
{
current_connection->start();
}
}
~async_tcp_server()
{
io_service_.stop();
}
private:
boost::asio::io_service io_service_;
boost::asio::ip::tcp::acceptor acceptor_;
};
void send_data(std::string const& filename, std::string const& adr = "localhost:6767")
{
try
{
boost::asio::io_service io_service;
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "Adress is: " << adr << " and file is: " << filename << '\n';
}
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << adr << '\n';
}
async_tcp_client client(io_service, adr, filename);
io_service.run();
}
catch(std::exception const& e)
{
std::cerr << "Exception in " << __PRETTY_FUNCTION__ << ": " << e.what() << "\n";
};
};
void rec_data(void)
{
try
{
{
boost::mutex::scoped_lock lk(debug_mutex);
std::cout << "Receiving data...\n";
}
async_tcp_server recv_file_tcp_server(6767);
if(debugmode)
{
boost::mutex::scoped_lock lk(debug_mutex);
debug_global << "Received\n";
}
}
catch(std::exception const& e)
{
std::cerr << "Exception in " << __PRETTY_FUNCTION__ << ": " << e.what() << "\n";
};
};
int main()
{
boost::thread_group g;
g.create_thread(rec_data); // get the receiver running
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
g.create_thread([] { send_data("main.cpp"); });
g.join_all();
}
My idea was to create X threads, run it using KeepRunning method which has endless loop calling _io_service.run() and send tasks to _io_service when received a new connection using _io_service.poll() in async_accept handler.
I run the server with a code like this:
oh::msg::OHServer s("0.0.0.0", "9999", 200);
ConsoleStopServer = boost::bind(&oh::msg::OHServer::Stop, &s);
SetConsoleCtrlHandler(bConsoleHandler, TRUE);
s.Run();
but when I receive one connection, then serve it in Post() method using blocking read/writes in MsgWorker class, then all the threads are being closed.
I have code like below (it's some mix from http server3 asio example and mine):
OHServer::OHServer(const std::string& sAddress, const std::string& sPort, std::size_t tps)
: _nThreadPoolSize(tps), _acceptor(_io_service), _sockClient(new boost::asio::ip::tcp::socket(_io_service))
{
// Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR).
boost::asio::ip::tcp::resolver resolver(_io_service);
boost::asio::ip::tcp::resolver::query query(sAddress, sPort);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
_acceptor.open(endpoint.protocol());
_acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
_acceptor.bind(endpoint);
_acceptor.listen();
_acceptor.async_accept(
*_sockClient,
boost::bind(
&OHServer::AcceptConnection,
this,
boost::asio::placeholders::error
)
);
}
void OHServer::KeepRunning()
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Start" << std::endl;
global_stream_lock.unlock();
while( true )
{
try
{
boost::system::error_code ec;
_io_service.run( ec );
if( ec )
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Error: " << ec << std::endl;
global_stream_lock.unlock();
}
break;
}
catch( std::exception & ex )
{
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Exception: " << ex.what() << std::endl;
global_stream_lock.unlock();
}
}
global_stream_lock.lock();
std::cout << "[" << boost::this_thread::get_id()
<< "] Thread Finish" << std::endl;
global_stream_lock.unlock();
}
void OHServer::Run()
{
// Create a pool of threads to run all of the io_services.
for (std::size_t i = 0; i < _nThreadPoolSize; ++i)
{
boost::shared_ptr<boost::thread> thread(new boost::thread(
boost::bind(&OHServer::KeepRunning, this)));
threads.push_back(thread);
}
cout << "Hit enter to close server" << endl;
cin.get();
}
void OHServer::Stop()
{
boost::system::error_code ec;
_acceptor.close(ec);
_sockClient->shutdown( boost::asio::ip::tcp::socket::shutdown_both, ec );
_sockClient->close( ec );
_io_service.stop();
// Wait for all threads in the pool to exit.
for (std::size_t i = 0; i < threads.size(); ++i)
{
threads[i]->join();
cout << "threads[ "<< i << "]->join();" << endl;
}
}
void OHServer::Post()
{
std::cout << "Accepted new connection." << std::endl;
CMsgWorker *msgWorker = new CMsgWorker(_sockClient);
msgWorker->Start();
delete msgWorker;
}
void OHServer::AcceptConnection(const boost::system::error_code& e)
{
if (!e)
{
_io_service.post(boost::bind(&OHServer::Post, this));
_acceptor.async_accept(
*_sockClient,
boost::bind(
&OHServer::AcceptConnection,
this,
boost::asio::placeholders::error
)
);
}
}
What should I do for the threads to be still waiting for some work to do from _io_service?
Thanks for any help!
Check it out:
// Kick off 5 threads
for (size_t i = 0; i < 5; ++i) {
boost::thread* t = threads.create_thread(boost::bind(&boost::asio::io_service::run, &io));
std::cout << "Creating thread " << i << " with id " << t->get_id() << std::endl;
}
See the timer.cc example here for an idea on how to do this: https://github.com/sean-/Boost.Examples/tree/master/asio/timer
Finally I've ended up with some easy-to-use version of server:
Usage:
boost::shared_ptr<CTCPServer> _serverPtr;
void CMyServer::Start()
{
//First we must create a few threads
thread* t = 0;
for (int i = 0; i < COHConfig::_iThreads; ++i)
{
t =_threads.create_thread(bind(&io_service::run, &_io_service));
}
//Then we create a server object
_serverPtr.reset( new CTCPServer(&_io_service, PORT_NUMBER) );
//And finally run the server through io_service
_io_service.post(boost::bind(&CMyServer::RunServer, _serverPtr, &CMyServer::HandleMessage));
}
//This is the function which is called by io_service to start our server
void CMyServer::RunServer(CTCPServer* s, void (*HandleFunction)(shared_ptr<ip::tcp::socket>, deadline_timer*))
{
s->Run(HandleFunction);
}
//And this is our connection handler
void CMyServer::HandleMessage(shared_ptr< ip::tcp::socket > sockClient, deadline_timer* timer)
{
cout << "Handling connection from: " << sockClient->remote_endpoint().address().to_string() << ":" << sockClient->remote_endpoint().port() << endl;
//This is some class which gets socket in its constructor and handles the connection
scoped_ptr<CMyWorker> myWorker( new CMyWorker(sockClient) );
msgWorker->Start();
}
//Thanks to this function we can stop our server
void CMyServer::Stop()
{
_serverPtr->Stop();
}
The TCPServer.hpp file:
#ifndef TCPSERVER_HPP
#define TCPSERVER_HPP
#if defined(_WIN32)
#define BOOST_THREAD_USE_LIB
#endif
#include <boost/asio.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <vector>
class CTCPServer: private boost::noncopyable
{
private:
bool bKeepRunning;
boost::asio::io_service* _io_service;
std::string _sPort;
boost::asio::ip::tcp::acceptor _acceptor;
boost::shared_ptr< boost::asio::ip::tcp::socket > _sockClient;
boost::asio::deadline_timer _timer;
bool _bIPv6;
std::string SessionID();
public:
CTCPServer(boost::asio::io_service* ios, const std::string& sPort, bool bIPv6=false):
_sPort(sPort),
_acceptor(*ios),
_timer(*ios),
_bIPv6(bIPv6)
{
_io_service = ios;
bKeepRunning = false;
};
void Run(void (*HandleFunction)(boost::shared_ptr< boost::asio::ip::tcp::socket > sock, boost::asio::deadline_timer* timer));
void AsyncAccept(void (*HandleFunction)(boost::shared_ptr< boost::asio::ip::tcp::socket > , boost::asio::deadline_timer* ));
void AcceptHandler(const boost::system::error_code& e, void (*HandleFunction)(boost::shared_ptr< boost::asio::ip::tcp::socket >, boost::asio::deadline_timer* ));
void Stop();
void Stop(void (*StopFunction)());
};
#endif
The TCPServer.cpp file:
#include "TCPServer.hpp"
#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread/mutex.hpp>
#include <iostream>
using namespace std;
string CTCPServer::SessionID()
{
ostringstream outs;
outs << "[" << boost::this_thread::get_id() << "] ";
return outs.str();
}
void CTCPServer::Run(void (*HandleFunction)(boost::shared_ptr< boost::asio::ip::tcp::socket > , boost::asio::deadline_timer* ))
{
try
{
boost::asio::ip::tcp::resolver resolver(*_io_service);
boost::asio::ip::tcp::endpoint endpoint;
if(_bIPv6)
{
boost::asio::ip::tcp::resolver::query queryv6(boost::asio::ip::tcp::v6(), _sPort);
endpoint = *resolver.resolve(queryv6);
}
else
{
boost::asio::ip::tcp::resolver::query queryv4(boost::asio::ip::tcp::v4(), _sPort);
endpoint = *resolver.resolve(queryv4);
}
_acceptor.open(endpoint.protocol());
_acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
_acceptor.set_option(boost::asio::socket_base::enable_connection_aborted(true));
_acceptor.bind(endpoint);
_acceptor.listen();
boost::system::error_code ec;
bKeepRunning = true;
AsyncAccept(HandleFunction);
}
catch(std::exception& e)
{
if(!_bIPv6)
std::cerr << "Exception wile creating IPv4 TCP socket on port "<< _sPort<< ": " << e.what() << std::endl;
else
std::cerr << "Exception wile creating IPv6 TCP socket on port "<< _sPort<< ": " << e.what() << std::endl;
}
}
void CTCPServer::AsyncAccept(void (*HandleFunction)(boost::shared_ptr< boost::asio::ip::tcp::socket > , boost::asio::deadline_timer* ))
{
if(bKeepRunning)
{
try
{
_sockClient.reset(new boost::asio::ip::tcp::socket(*_io_service));
cout << SessionID() << "Waiting for connection on port: " << _sPort << endl;
_acceptor.async_accept(*_sockClient, boost::bind(&CTCPServer::AcceptHandler, this, boost::asio::placeholders::error, HandleFunction));
}
catch(exception& e)
{
string sWhat = e.what();
cout << SessionID() << "Error while accepting connection: " << e.what() << endl;
}
}
}
void CTCPServer::AcceptHandler(const boost::system::error_code& e,
void (*HandleFunction)(boost::shared_ptr< boost::asio::ip::tcp::socket >,
boost::asio::deadline_timer* ))
{
if(!e)
{
try
{
(*_io_service).post(boost::bind(HandleFunction, _sockClient, &_timer));
AsyncAccept(HandleFunction);
}
catch(exception& e)
{
cout << SessionID() << "Exception: " << e.what() << endl;
}
}
}
void CTCPServer::Stop()
{
cout << SessionID() << "STOP port " << _sPort << endl;
if(!bKeepRunning)
return;
bKeepRunning = false;
try
{
_sockClient->close();
}
catch(exception& e)
{
cout << SessionID() << "Exception: " << e.what() << endl;
}
try
{
_acceptor.cancel();
}
catch(exception& e)
{
cout << SessionID() << "Exception: " << e.what() << endl;
}
try
{
_acceptor.close();
}
catch(exception& e)
{
cout << SessionID() << "Exception: " << e.what() << endl;
}
}
void CTCPServer::Stop(void (*StopFunction)())
{
Stop();
StopFunction();
}
It's also very easy to modify to be IPv6 compatible.
It's already tested and working very well. Just copy it and use!