Hi everyone, this (I think) is about boost::asio. Could somebody please tell
me why in the following code my handle_write function is never called after
the async_write? This tries to be an hybrid mixture of sync read and async
write through a socket and even though the string gets written, no handler
is called at the end.
#include
#include
#include
#include
#include
#include <iostream>
#include <string>
using boost::asio::ip::tcp;
using namespace std;
class Server : public boost::enable_shared_from_this<Server> {
public:
Server();
virtual ~Server();
void run(tcp::socket &);
void handle_write(const boost::system::error_code &, size_t);
};
Server::Server() {
}
Server::~Server() {
}
void Server::run(tcp::socket & sock) {
string message("Hi!");
try {
async_write(sock, boost::asio::buffer(message), boost::bind(
&Server::handle_write,
shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
} catch (exception& e) {
cerr << e.what() << endl;
}
return;
}
void Server::handle_write(const boost::system::error_code & errc, size_t
bytes) {
cout << "Function called" << endl;
return;
}
int main() {
try {
boost::asio::io_service ioserv;
tcp::acceptor acceptor(ioserv, tcp::endpoint(tcp::v4(), 1337));
boost::shared_ptr<Server> newserver(new Server());
for (;;) {
tcp::socket newsocket(ioserv);
acceptor.accept(newsocket);
cout << "Somebody arrived" << endl;
newserver->run(newsocket);
}
} catch (exception& e) {
cerr << e.what() << endl;
}
return 0;
}