1

I have one problem.

I'm developing chat server, using boost::asio.

and Here,

void CServerSocket::StartAccept(boost::asio::ip::tcp::acceptor &acceptor)
{
    std::shared_ptr<boost::asio::ip::tcp::socket> socket(new boost::asio::ip::tcp::socket(acceptor.get_io_service()));

    acceptor.async_accept(*socket, std::bind(&CServerSocket::OnAccept, boost::asio::placeholders::error, socket,
        std::ref(acceptor)));
}

void CServerSocket::OnAccept(const boost::system::error_code &error, std::shared_ptr<boost::asio::ip::tcp::socket> socket, 
    boost::asio::ip::tcp::acceptor &acceptor)
{
    if (error)
    {
        CLogManager::WriteLog((boost::format("Accept error! : %1%") % error.message()).str().c_str());
        return;
    }

    m_SocketList.push_back(std::make_shared<CConnectionSocket>(this, socket));

    StartAccept(acceptor);
}

At std::bind, there are an error occurred.

"Error c2064 term does not evaluate to a function taking 3 arguments"

What should i do?

thanks.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
BombPenguin
  • 211
  • 2
  • 3
  • 10
  • one error is you forgot the instance to bind with. you need a **this** as second parameter. `std::bind(&CServerSocket::OnAccept, this, boost::asio::placeholders::error, socket, std::ref(acceptor))` – user1810087 Dec 19 '13 at 22:43
  • I've already tried. but, there is an another error occurred. Error 1 error C2664: '_Rx std::_Pmf_wrap<_Pmf_t,_Rx,_Farg0,_V0_t,_V1_t,_V2_t,_V3_t,_V4_t,_V5_t,>::operator ()(const _Wrapper &,_V0_t,_V1_t,_V2_t) const' : cannot cast 'boost::arg' to 'const boost::system::error_code &'. c:\program files (x86)\microsoft visual studio 11.0\vc\include\functional 1152 – BombPenguin Dec 19 '13 at 22:48
  • umm thats the second error, i think... you cannot use std::bind with std::shared_ptr but boost::asio::placeholders. see [here](http://stackoverflow.com/a/9536984/1810087) – user1810087 Dec 19 '13 at 22:53

2 Answers2

3

If you're using std::bind, replace boost::asio::placeholders::error with std::placeholders::_1.

Igor R.
  • 14,716
  • 2
  • 49
  • 83
-1

An accept handler may only take an error code as a parameter, see: AcceptHandler.

I recommend making acceptor a member of CServerSocket then changing the call to async_accept to:

acceptor.async_accept(*socket, std::bind(&CServerSocket::OnAccept, this,
                                         std::placeholders::_1));

and accessing acceptor within the OnAccept member function.

kenba
  • 4,303
  • 1
  • 23
  • 40
  • That's incorrect. See [std::bind documentation](http://en.cppreference.com/w/cpp/utility/functional/bind). – Igor R. Dec 20 '13 at 10:05
  • Thank you for spotting my mistake @Igor. I've amended my answer to use a `std::placeholder` instead of the `boost::placeholder` from the asio documentation. – kenba Dec 20 '13 at 10:39
  • You answer still implies that it's impossible (or incorrect) to pass to `CServerSocket::OnAccept` extra parameters - which is incorrect as well. – Igor R. Dec 20 '13 at 12:13