This is an example to check connection state for a specific port number. It returns command output.
void* check_connection_state(void *data) { uint32_t counter = 0; char buf[512] = {0}; FILE *p = popen("netstat -an | grep -i PORT_NUM", "r"); while(counter > 120 || (fread(buf, 1, sizeof(buf), p) != 0)) { counter++; pclose(p); sleep(1); p = popen("netstat -an | grep -i PORT_NUM", "r"); } pthread_exit(0); } int main(int argc, char* argv[]) { // Check connection pthread_t check_connection_thread; pthread_create (&check_connection_thread, NULL, check_connection_state, NULL); pthread_join(check_connection_thread, NULL); return 0; }