
Hi Community, I've a problem with named-pipe communication. Please help me investigate this. I'm totally clueless now. I have a Connector class. I want to read some data from a socket. This is it: class Connector { public: Connector(boost::asio::io_service& io_service) : _io_service(io_service), _socket(io_service) { _endpoint.address(boost::asio::ip::address::from_string("127.0.0.1")); _endpoint.port(4242); _socket.async_connect(_endpoint, boost::bind(&Connector::handle_accept, this, boost::asio::placeholders::error)); } void close() { _io_service.post(boost::bind(&Connector::do_close, this)); _io_service.stop(); } private: void handle_accept(const boost::system::error_code& error) { if (error) { throw ConnectorError(_T("Cannot estabilish connection.")); } boost::asio::async_read(_socket, boost::asio::buffer(_readbuffer, 1024), boost::bind(&Connector::handle_read, this, boost::asio::placeholders::error)); } void handle_read(const boost::system::error_code& error) { if (!error) { if (_readbuffer.empty()) { boost::this_thread::sleep(boost::posix_time::milliseconds(100)); } else { std::string string_buffer; std::copy(_readbuffer.begin(), _readbuffer.end(), std::back_insert_iteratorstd::string(string_buffer)); std::cout << string_buffer << std::endl; _readbuffer.clear(); } boost::asio::async_read(_socket, boost::asio::buffer(_readbuffer, 1024), boost::bind(&Connector::handle_read, this, boost::asio::placeholders::error)); } } void do_close() { _socket.close(); } boost::asio::io_service& _io_service; boost::asio::ip::tcp::socket _socket; boost::asio::ip::tcp::endpoint _endpoint; std::vector<char> _readbuffer; }; This is how I use it: boost::asio::io_service io_service; Connector connector(io_service); boost::thread async_io_thread(boost::bind(&boost::asio::io_service::run, &io_service)); while (1) { //... stuff } connector.close(); async_io_thread.join(); But whenever I send a data package, this class is unable to read it from the socket. The readbuffer is always empty. I have a python code to test the sending application. import sys import socket s = socket.socket() address = '127.0.0.1' port = 4242 try: s.connect((address, port)) except Exception, e: sys.stdout.write('conn err %s:%d. exception: %s' % (address, port, `e`)) try: data = s.recv(1024) print data while True: more = s.recv(1024) print more if not more: break except Exception, e: sys.stdout.write('read err %s:%d. exception: is %s' % (address, port, `e`)) It works perfectly. As far as I know these the two code snippet should work the same. Clearly I'm missing something. Regards, Miklos

On 01/08/2014 04:15 PM, Miklós Tóth wrote:
Connector(boost::asio::io_service& io_service) : _io_service(io_service), _socket(io_service) {
[...]
std::vector<char> _readbuffer; };
Boost.Asio assumes that your buffer is allocated with the correct size. It does not use the fact that std::vector can be dynamically enlarged. Add _readbuffer(1024) to your initializer list.

It works! I had never figured it out. Thanks.
2014/1/8 Bjorn Reese
On 01/08/2014 04:15 PM, Miklós Tóth wrote:
Connector(boost::asio::io_service& io_service)
: _io_service(io_service), _socket(io_service) {
[...]
std::vector<char> _readbuffer; };
Boost.Asio assumes that your buffer is allocated with the correct size. It does not use the fact that std::vector can be dynamically enlarged.
Add _readbuffer(1024) to your initializer list.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Bjorn Reese
-
Miklós Tóth