Hi all,
I'm experimenting with Boost::asio, and I'm trying to make a client that reads and outputs to console packets sent from a server. The server uses a proprietary protolcol. It sends a timer update every second, responds to ping, and can reply with a list of files when the client asks for it. I have a decent grasp of asynchronous networking, but I'm having a problem. Here's the code:
class JReader
{
public:
JReader(boost::asio::io_service &io_s)
: socket_(io_s)
{
tcp::resolver resolver(io_s);
tcp::resolver::query query("xxxx", "yyyy");
tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
tcp::resolver::iterator end;
boost::system::error_code error = boost::asio::error::host_not_found;
while (error && endpoint_iterator != end)
{
socket_.close();
socket_.connect(*endpoint_iterator++, error);
}
if (error)
{
throw boost::system::system_error(error);
}
boost::asio::async_write(socket_, boost::asio::buffer(connect, 12), boost::bind(&JReader::handle_write, this));
boost::asio::async_write(socket_, boost::asio::buffer(getlist, 3), boost::bind(&JReader::handle_write, this));
boost::asio::async_read(socket_, boost::asio::buffer(buf_, BUF_SIZE), boost::bind(&JReader::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
private:
void handle_read(const boost::system::error_code err, size_t bytes)
{
std::cout<<std::endl<<std::string(buf_, bytes);
boost::asio::async_read(socket_, boost::asio::buffer(buf_, BUF_SIZE), boost::bind(&JReader::handle_read, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void handle_write()
{
//TODO: finish this!
std::cout<<std::endl<<"Wrote something";
}
static const int BUF_SIZE = 256;
char buf_[BUF_SIZE];
tcp::socket socket_;
};
The program reads from the server and outputs data to console -- but the motherboard makes a terrible beeping sound. I'm pretty sure it has something to do with my handle_read loop.
What is the correct, nonblocking way of continually listening to a server?