Boost logo

Boost Users :

Subject: [Boost-users] [boost-users][asio] Can't read socket
From: Miklós Tóth (spiralfuzet_at_[hidden])
Date: 2014-01-08 10:15:19


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_iterator<std::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



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net