I am developing a tiny server loop for a bigger software but it doesn't work as I want it to do.
When the user types in ".quit" I want the software to stop this threaded 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 thread the following way:
serverThread = boost::shared_ptr<boost::thread>( new boost::thread(boost::bind( &myClass::startServer, this )) );
But I have problems stopping the "server". No matter if I interrupt the thread, close the socket and/or the acceptor or just break the program Boost throws the error:
Bad file descriptor
It doesn't occurs every time but often and I want to fix that issue and not just ignore it.
Can you help me how to shut this down clean?