简体   繁体   中英

C++ boost tcp-server

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.

Seems like something went wrong there and i dont really know whats missing.

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".

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.

Thanks in advance.

tcp_server.h

#include <cstdlib>
#include <iostream>
#include <exception>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>

using boost::asio::ip::tcp;

class tcp_server
{
public:
typedef boost::shared_ptr<tcp::socket> socket_ptr;

tcp_server();
~tcp_server();

void server(boost::asio::io_service&);

private:

const short port = 8888;
const int max_length = 1024;

void session(socket_ptr);

};

tcp_server.cpp

#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->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& e)
  {
    std::cerr << "Exception in thread: " << e.what() << "\n"; //
  }
}


void tcp_server::server(boost::asio::io_service& 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(<unresolved overloaded function type>, tcp_server::socket_ptr&)’`
  }
}

main.cpp

#include "tcp_server.h"


int main() {

    tcp_server server;

    try
  {
    boost::asio::io_service io_service;

    server.server(io_service);
  }
  catch (std::exception& e)
  {
    std::cerr << "Exception: " << e.what() << "\n";
  }

  return 0;
}

You cannot bind the non-static member function tcp_server::session because it cannot be called without an object instance, which is not available to the bind.

You can however make tcp_server::session static (in tcp_header.h) to fix this:

static void session(socket_ptr);

Edit:

You can bind session as non-static member using:

boost::thread t(boost::bind(&tcp_server::session, this, sock));

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