-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathTimeoutRecvfromClientBySockOpt.c
92 lines (78 loc) · 2.27 KB
/
TimeoutRecvfromClientBySockOpt.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
int main(int argc, char const *argv[])
{
if (argc != 3 && argc != 4) {
printf("usage : %s <ip/hostname> <port/service> [recv timeout]\n", argv[0]);
exit(1);
}
setbuf(stdout, NULL);
const char *hostname = argv[1];
const char *service = argv[2];
int recv_timeout = 5;
if (argc == 4)
recv_timeout = atoi(argv[3]);
struct addrinfo hints, *res = NULL, *ressave = NULL;
bzero(&hints, sizeof(hints));
hints.ai_socktype = SOCK_DGRAM;
hints.ai_family = AF_INET;
int error = 0;
if ((error = getaddrinfo(hostname, service, &hints, &res)) != 0) {
printf("getaddrinfo error! %s %s : %s\n", hostname, service, gai_strerror(error));
exit(1);
}
ressave = res;
int udpfd = -1;
do {
if ((udpfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0) {
perror("socket error!");
continue;
}
struct timeval tv;
socklen_t len = sizeof(tv);
if (getsockopt(udpfd, SOL_SOCKET, SO_RCVTIMEO, &tv, &len) < 0) {
perror("getsockopt error!");
}
printf("default recv timeout = %ld\n", tv.tv_sec);
tv.tv_sec = recv_timeout;
tv.tv_usec = 0;
if (setsockopt(udpfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) {
perror("socket fd set socket opt error!");
goto udp_fd_err;
}
if (connect(udpfd, res->ai_addr, res->ai_addrlen) < 0) {
perror("connect error!");
goto udp_fd_err;
}
break;
udp_fd_err:
close(udpfd);
udpfd = -1;
} while ((res = res->ai_next) != NULL);
if (res == NULL) {
printf("can't assign a socket address\n");
exit(1);
}
char c = 1;
if (write(udpfd, &c, 1) != 1) {
perror("write error!");
exit(1);
}
char buf[1024];
ssize_t nread = 0;
if ((nread = read(udpfd, buf, sizeof(buf))) <= 0) {
if (errno == EWOULDBLOCK)
errno = ETIMEDOUT;
perror("read error!");
exit(1);
}
buf[nread] = 0;
printf("%s\n", buf);
return 0;
}