I'm sending an array of floats, one by one, over a TCP socket . My Server (receiver - which handles multiple requests simultaneously) program should read data until until receiving the value 0. If the client(sender) doesn't send anything for 10 seconds after he connected (or after the last sent value) i want the server to close that connection. I found this signal approach but I think it will not be optimal to use in threads, but more likely for fork() because it forces me to use global variables. I must send the "socket" param to the function, so i can close it and AFAIK this is not posible.
void time_out(int semnal) {
printf("Time out.\n");
close(socket);
exit(1);
}
and each time a client has connected, or sends something, I call this:
signal(SIGALRM, time_out);
alarm(10);
What other options do i have to count 10 seconds and to be able to restart this timer?
You could use select with an empty FD set and a timeout. That's pretty common and pretty portable.