[asio] Callbacks not getting called

I posted something about this a couple days ago, but it was fairly complex, and had some typos in it. Here's another try, where I've trimmed it down even more, trying to get to the meat of the problem. I'm just using c-style functions for the callback now and its still not working. What I am doing: 1. Start a server process 2. The server process makes an empty socket and uses it to listen (with a tcp::acceptor) for TCP connections on a port (10010 for example) 3. Start a client process 4. Have the client process create a socket connect to the server's port 5. When the server sees a client is connecting, it starts listening for data(with async_read) on the socket and creates another empty socket to listen for another TCP connection on the port 6. When the client sees that the server has connected, it sends 100 bytes of data (with async_write) and waits for the socket to tell it the send is finished...when that happens it prints a message and shuts down 7. When the server gets notified that its has data that has been read, it prints a message and shuts down I'm running on windows (originally with boost_1_38_0, but today I tired with boost_1_42_0 with the same results) and have a visual studio solution file you can get (http://boost.teeks99.com/BoostConnectionTest.zip) if you're interested. BoostConnectionServer.cpp: #include <boost/asio.hpp> #include <boost/thread.hpp> #include <iostream> using namespace boost::asio::ip; boost::asio::io_service* io_service_; tcp::acceptor* acceptor_; tcp::socket* sock1_; tcp::socket* sock2_; bool conn1Done_; bool gotTransfer_; void HandleAccept(const boost::system::error_code& error) { conn1Done_ = true; sock2_ = new tcp::socket(*io_service_); acceptor_->async_accept(*sock2_, HandleAccept); } // Doesn't compile with one param like the chat example //void HandleTransfer(const boost::system::error_code& error) // Compiles with 2 params, but never gets called void HandleTransfer(const boost::system::error_code& error, size_t bytes) { if(!error) gotTransfer_; else std::cout << error << std::endl; } int main(int argc, char* argv[]) { io_service_ = new boost::asio::io_service(); conn1Done_ = false; gotTransfer_ = false; sock1_ = new tcp::socket(*io_service_); tcp::endpoint endpoint(tcp::v4(), 10011); acceptor_ = new tcp::acceptor(*io_service_, endpoint); acceptor_->async_accept(*sock1_, HandleAccept); boost::thread t(boost::bind(&boost::asio::io_service::run, io_service_)); while(!conn1Done_) { boost::this_thread::sleep(boost::posix_time::millisec(1)); } std::cout << "Accepted incoming connection\n"; char data[100]; boost::asio::async_read(*sock1_, boost::asio::buffer(data, 100), HandleTransfer); while(!gotTransfer_) { boost::this_thread::sleep(boost::posix_time::millisec(1)); } std::cout << "Success!\n"; return 0; } BoostConnectionClient.cpp: #include <boost/asio.hpp> #include <boost/thread.hpp> #include <iostream> using namespace boost::asio::ip; boost::asio::io_service* io_service_; tcp::socket* sock_; bool connected_; bool gotTransfer_; void HandleConnect(const boost::system::error_code& error) { if(!error) connected_ = true; else std::cout << error << std::endl; } // Doesn't compile with one param like the chat example //void HandleTransfer(const boost::system::error_code& error) // Compiles with 2 params, but never gets called void HandleTransfer(const boost::system::error_code& error, size_t bytes) { if(!error) gotTransfer_; else std::cout << error << std::endl; } int main(int argc, char* argv[]) { connected_ = false; gotTransfer_ = false; io_service_ = new boost::asio::io_service(); sock_ = new tcp::socket(*io_service_); tcp::endpoint ep(address::from_string("127.0.0.1"), 10011); sock_->async_connect(ep, HandleConnect); boost::thread t(boost::bind(&boost::asio::io_service::run, io_service_)); while(!connected_) { boost::this_thread::sleep(boost::posix_time::millisec(1)); } std::cout << "Connected\n"; char data[100]; data[0] = 0; data[1] = 1; data[2] = 2; data[3] = 3; data[99] = 99; boost::asio::async_write(*sock_, boost::asio::buffer(data, 100), HandleTransfer); while(!gotTransfer_) { boost::this_thread::sleep(boost::posix_time::millisec(1)); } std::cout << "Done\n"; return 0; } Any thoughts on what I'm doing incorrectly here would be much appreciated. Thanks in advance :-) Tom Kent
participants (2)
-
Rutger ter Borg
-
Tom Kent