title: C++ boost tcp-server
body: <p>I tried to create an tcp-server with the boost libs. I basically took example code from boost(the whole code was in 1 file there) and split it up into a class.</p>
  
  <p>Seems like something went wrong there and i dont really know whats missing.</p>
  
  <p>The main error is at the end of "tcp_server.cpp" i put the error message into a comment there. It looks like he doesnt know the function session(at least thats how i interpreted it), i get the same error with "tcp_server::session" and "this->session".</p>
  
  <p>The second thing is an error i get from "e.what()"(cannot resolve identifer what). It worked in the example code and i didnt change anything at the exceptions.</p>
  
  <p>Thanks in advance.</p>
  
  <p>tcp_server.h</p>
  
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;exception&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/smart_ptr.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/thread/thread.hpp&gt;
  
  using boost::asio::ip::tcp;
  
  class tcp_server
  {
  public:
  typedef boost::shared_ptr&lt;tcp::socket&gt; socket_ptr;
  
  tcp_server();
  ~tcp_server();
  
  void server(boost::asio::io_service&amp;);
  
  private:
  
  const short port = 8888;
  const int max_length = 1024;
  
  void session(socket_ptr);
  
  };
  </code></pre>
  
  <p>tcp_server.cpp</p>
  
  <pre><code>#include "tcp_server.h"
  
  
  tcp_server::tcp_server()
  {
  
  }
  
  tcp_server::~tcp_server()
  {
  
  }
  
  
  void tcp_server::session(socket_ptr sock)
  {
  try
    {
      for (;;)
      {
        char data[max_length];
  
        boost::system::error_code error;
        size_t length = sock-&gt;read_some(boost::asio::buffer(data), error);
  
  
        if (error == boost::asio::error::eof)
          break; // Connection closed cleanly by peer.
        else if (error)
          throw boost::system::system_error(error); // Some other error.
  
        boost::asio::write(*sock, boost::asio::buffer(data, length));
      }
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; "Exception in thread: " &lt;&lt; e.what() &lt;&lt; "\n"; //
    }
  }
  
  
  void tcp_server::server(boost::asio::io_service&amp; io_service)
  {
      tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
    for (;;)
    {
      socket_ptr sock(new tcp::socket(io_service));
      a.accept(*sock);
      boost::thread t(boost::bind(session, sock)); //error: no matching function for call to ‘bind(&lt;unresolved overloaded function type&gt;, tcp_server::socket_ptr&amp;)’`
    }
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "tcp_server.h"
  
  
  int main() {
  
      tcp_server server;
  
      try
    {
      boost::asio::io_service io_service;
  
      server.server(io_service);
    }
    catch (std::exception&amp; e)
    {
      std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
    }
  
    return 0;
  }
  </code></pre>