I have following server code. which waits for client to connect and once the client connects it will start thread which receives data from client connected and main thread will wait for another client to connect. This code works fine.
Now I have to specify that server will wait for some time say 10 sec to receive data from connected client. or otherwise server will close the communication if no data received within specified time. I have implemented timer for the same but somehow it doesnt work and timer_callback is not called after time elapsed.
#include <ctime>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <sys/socket.h>
#include <unistd.h>
#include <string>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/date_time.hpp>
using namespace std;
using boost::asio::ip::tcp;
void run(boost::shared_ptr<tcp::socket> my_socket)
{
while (1)
{
char buf[128];
boost::system::error_code error;
size_t len = my_socket->read_some(boost::asio::buffer(buf, 128), error);
std::cout << "len : " << len << std::endl;
if (error == boost::asio::error::eof)
{
cout << "\t(boost::asio::error::eof)" << endl;
if (my_socket->is_open())
{
boost::system::error_code ec;
cout << "\tSocket closing" << endl;
my_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both, ec);
cout << "\tShutdown " << ec.message() << endl;
//cout << "normal close : " << ::close(my_socket->native_handle()) << endl;
my_socket->close(ec);
cout << "\tSocket closed" << endl;
}
break; // Connection closed cleanly by peer.
}
else if (error)
{
std::cout << "Exception : " << error.message() << std::endl;
break;
}
else
{
for (unsigned int i = 0; i < len; i++)
printf("%02x ", buf[i] & 0xFF);
printf("\n");
}
}
}
void timer_callback(boost::shared_ptr<tcp::socket> my_socket, const boost::system::error_code& error)
{
std::cout << "timer expired.... " << std::endl;
if (error != boost::asio::error::operation_aborted)
{
// do something
std::string str_error = "timer expired " + error.message();
my_socket->shutdown(boost::asio::ip::tcp::socket::shutdown_both);
my_socket->close();
}
else
{
std::cout << "timer cancelled ..." << error.message() << std::endl;
}
}
int main()
{
const int S = 1000;
vector<boost::shared_ptr<boost::thread> > arr_thr(S);
boost::asio::io_service io_service;
boost::asio::deadline_timer timer(io_service);
try
{
for (uint32_t i = 0;; i++)
{
tcp::endpoint endpoint(tcp::v6(), 10001);
boost::shared_ptr<tcp::socket> my_socket(new tcp::socket(io_service));
tcp::endpoint end_type;
tcp::acceptor acceptor(io_service, endpoint);
std::cout << "before accept" << endl;
acceptor.accept(*my_socket, end_type);
usleep(10000);
std::cout << "connected... hdl : " << my_socket->native_handle() << std::endl;
boost::asio::ip::address addr = end_type.address();
std::string sClientIp = addr.to_string();
std::cout << "\tclient IP : " << sClientIp << std::endl;
timer.expires_from_now(boost::posix_time::seconds(10));
timer.async_wait(
boost::bind(&timer_callback, my_socket, boost::asio::placeholders::error));
arr_thr[i] = boost::shared_ptr<boost::thread>(new boost::thread(&run, my_socket));
}
} catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
It looks like you want to do the
acceptandasync_waitfrom the main thread and do some blocking read on the separate thread.There are multiple problems with that approach. First your
acceptis blocking, so the timer can’t run while the main thread waits for an accept. Also there is no call toio_servicerunorpoll, so the async handler will never be called.You have to use
async_acceptif you want to mix it withasync_waitin the same thread.However you still can’t cancel a blocking read on another thread. The documentation states that it’s unsafe to use a socket from multiple threads simultaneously (Shared objects: Unsafe).
I would recommend that you use a fully asynchronous design.