-
Notifications
You must be signed in to change notification settings - Fork 1
/
timer.c
31 lines (28 loc) · 761 Bytes
/
timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <sys/time.h>
#include <sys/socket.h>
// Sets timeout value for socket 'sock' for 'timeout_val'
// (in seconds)
int set_timeout(int sock, int timeout_val)
{
struct timeval timeout;
timeout.tv_sec = timeout_val;
timeout.tv_usec = 0;
int ret = setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
if (ret == -1) {
fprintf(stderr, "[set_timeout]: couldn't set timeout for sock\n");
return -1;
} else {
return 0;
}
}
// Disables timeout for socket 'sock'
int disable_timeout(int sock)
{
if (set_timeout(sock, 0) == -1) {
fprintf(stderr, "[disable_timeout]: couldn't disable timeout for sock\n");
return -1;
} else {
return 0;
}
}