简体   繁体   中英

Boost socket read functions not working

I am trying to write a C++ code (VS2008) that will open a socket via Boost and run a user-specified command through it. So far I have this:

#include <boost/asio.hpp>
#include <boost/array.hpp>
#include <boost/bind.hpp>
#include <string>
#define MAXSIZE 1000000
//...
void MyClass::processCommand(std::string command)
{
  boost::asio::io_service io;
  boost::asio::ip::tcp::socket socket(io);
  boost::asio::ip::tcp::endpoint e(boost::asio::ip::address::from_string("127.0.0.1"), 60151);  //Info for the connection I need to make...
  this->socket.open(boost::asio::ip::tcp::v4());
  this->socket.connect(e);
  this->socket.write_some(boost::asio::buffer(command, command.size());
  this->socket.send(boost::asio::buffer(command, command.size());

  boost::array<char, MAXSIZE> buffer;
  this->socket.async_read_some(boost::asio::buffer(b, MAXSIZE),
                             boost::bind(MyClass::handle_read, this,
                             boost::asio::placeholders::error,
                             boost::asio::placeholders::bytes_transferred));
}

void MyClass::handle_read(const boost::system::error_code& error, size_t bytes_transferred)
{
    //Haven't worked this part out yet...
}

I tried to model my code after the example on Boost's website here . However when I try to compile this I get a ton of error messages, mostly about bind expecting a different number of arguments. I'm really new to using Boost and honestly a little lost how to do this, so I'm not sure what to do to rectify this, since it seems to match the example on their site. I also tried to do this code with Boost's read_some() function, the code for which can be found in a question I posted about that method here .

Basically, I'm just asking what I could do to get this code to function as intended. I'm not too concerned which of the two functions I need to end up using (read_some() or async_read_some()) just as long as it works. If the other way is better, I'd greatly appreciate any help with figuring out the issue on that question. If this way is better and anyone knows how to fix it, that would be great too. Thanks a lot in advance.

EDIT : I accidentally forgot before to include the error messages I was getting. There were a lot of them (~70), all relating to boost::bind , and followed this syntax:

error: C2780: 'boost::_bi::bind_t<Rt2,boost::_mfi::cmf8<R,T,B1,B2,B3,B4,B5,B6,B7,B8>,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(boost::type<T>,R (__cdecl T::* )(B1,B2,B3,B4,B5,B6,B7,B8) const,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 11 arguments - 4 provided
error: C2780: 'boost::_bi::bind_t<Rt2,boost::_mfi::cmf7<R,T,B1,B2,B3,B4,B5,B6,B7>,_bi::list_av_8<A1,A2,A3,A4,A5,A6,A7,A8>::type> boost::bind(boost::type<T>,R (__cdecl T::* )(B1,B2,B3,B4,B5,B6,B7) const,A1,A2,A3,A4,A5,A6,A7,A8)' : expects 10 arguments - 4 provided
//etc.

It has a few of these with identical messages but saying it expected different numbers of arguments, from 11 down to 3. It also seemed to give the same general set of error messages 3 times for different formats. I'll give one example of each, since again all the others in that set of error messages are the same syntax, just for different numbers of arguments:

//first is same as above
error: C2780: 'boost::_bi::bind_t<Rt2,boost::_mfi::cmf8<R,T,B1,B2,B3,B4,B5,B6,B7,B8>,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(boost::type<T>,R (__cdecl T::* )(B1,B2,B3,B4,B5,B6,B7,B8) const,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 11 arguments - 4 provided


error: C2780: 'boost::_bi::bind_t<R,boost::_mfi::cmf8<R,T,A1,A2,A3,A4,A5,A6,A7,A8>,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(R (__cdecl T::* )(B1,B2,B3,B4,B5,B6,B7,B8) const,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 10 arguments - 4 provided


error: C2780: 'boost::_bi::bind_t<R,R(__cdecl *)(B1,B2,B3,B4,B5,B6,B7,B8,B9),_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(R (__cdecl *)(B1,B2,B3,B4,B5,B6,B7,B8,B9),A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 10 arguments - 4 provided


error: C2780: 'boost::_bi::bind_t<boost::_bi::unspecified,F,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(F,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 10 arguments - 4 provided


error: C2780: 'boost::_bi::bind_t<R,F,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(boost::type<T>,F,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 11 arguments - 4 provided

error: C2780: 'boost::_bi::bind_t<R,F,_bi::list_av_9<A1,A2,A3,A4,A5,A6,A7,A8,A9>::type> boost::bind(F,A1,A2,A3,A4,A5,A6,A7,A8,A9)' : expects 10 arguments - 4 provided

I also took screenshots of the full error log (or at least as much of it as I could get to fit on the screen at once) which can be seen below: 错误记录续

Take the address of handle_read when passing it to bind

this->socket.async_read_some(boost::asio::buffer(b, MAXSIZE),
                             boost::bind(&MyClass::handle_read, this,
                                     //  ^ 
                             boost::asio::placeholders::error,
                             boost::asio::placeholders::bytes_transferred));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM