Stop loop loop using Boost :: Asio

I am developing a tiny server loop for larger software, but it does not work the way I want it.

When the user enters ".quit", I want the software to stop this streaming server loop:

try {

while (true) {
      acceptor.accept(socket);
      const size_t buffersize = 1024;
      char data[buffersize+1] = {0};
      data[socket.read_some(boost::asio::buffer(data,buffersize))] = '\0'; // Write data & place terminator
      boost::thread asyncWriting(boost::bind( &myClass::writeToFile, this ));
      socket.close();
}

} catch(const boost::system::system_error& e) {
    cout << "Boost System Error: " << e.what() << endl;
}

I start the stream as follows:

serverThread = boost::shared_ptr<boost::thread>( new boost::thread(boost::bind( &myClass::startServer, this )) );

But I have problems stopping the "server". Regardless of whether I interrupt the flow, close the socket and / or acceptor or just break the Boost program, it gives an error:

 Bad file descriptor

This does not happen every time, but often, and I want to fix this problem, and not just ignore it.

Can you help me how to close this? "

+5
source share
1 answer

  • async_accept
  • async_read
  • async_write

- stop io_service. , ~io_service

shared_ptr < > . ( ), shared_ptr .

, , . , valgrind, , , .

,

+2

All Articles