My question title should be enough. I already tried (without success):
- Using a C-style destructor in a function:
__attribute__((destructor))
:
void sendToServerAtExit() __attribute__((destructor)) { mySocket->write("$%BYE_CODE%$"); }
The application destructor is called, but the socket is already disconnected and I can't write to the server.
- Using the standard C function
atexit()
, but the TCP connection is already lost so I can't send anything to the server.
atexit(sendToServerAtExit); // is the same function of point 1
The solution I found is check every second if all connected sockets are still connected, but I don't want to do so inefficient thing. It's only a temporary solution. Also, I want that others apps (even web ones) can join the chat room of my console app, and I don't want to request data every second.
What should I do?