You may install a signal handler to catch the application kill, and close the active connections from that handler:
#include "signal.h"
#include "stdio.h"
#include "stdlib.h"
void catch_sigint( int sig )
{
/* Close connections */
/*
* The software won't exit as you caught SIGINT
* so explicitly close the process when you're done
*/
exit( EXIT_SUCCESS );
}
int main( void )
{
/* Catches SIGINT (ctrl-c, for instance) */
if( signal( SIGINT, catch_sigint ) == SIG_ERR )
{
/* Failed to install the signal handler */
return EXIT_FAILURE;
}
/* Perform your operations */
while( 1 );
return EXIT_SUCCESS;
}