In my Client-Server program, I am creating a class which opens and uses several file descriptors for reading/writing to ports. In the class destructor, I call close( int ) for all of the descriptors.
My problem is if I abruptly end the program by pressing "Ctrl+Z", for example, the destructor is not called. Hence the sockets are not closed, and the ports are still occupied. As a result, I have to close and re-open Putty just so I can use the same ports again.
Is there way to call the destructor, or at least close all the descriptors, whenever I end the program by pressing "Ctrl+Z" or in some other similar manner?
Ctrl+Z sends signal SIGTSTP that will just pause the process by default. If the process receives SIGCONT it will resume execution from where it was paused.
If you want to handle SIGTSTP differently you can setup a signal handler to handle SIGTSTP the way you want.