forked from unpbook/unpv13e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dgclibcast3.c
53 lines (43 loc) · 1.16 KB
/
dgclibcast3.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
#include "unp.h"
static void recvfrom_alarm(int);
void
dg_cli(FILE *fp, int sockfd, const SA *pservaddr, socklen_t servlen)
{
int n;
const int on = 1;
char sendline[MAXLINE], recvline[MAXLINE + 1];
sigset_t sigset_alrm;
socklen_t len;
struct sockaddr *preply_addr;
preply_addr = Malloc(servlen);
Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
Sigemptyset(&sigset_alrm);
Sigaddset(&sigset_alrm, SIGALRM);
Signal(SIGALRM, recvfrom_alarm);
while (Fgets(sendline, MAXLINE, fp) != NULL) {
Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
alarm(5);
for ( ; ; ) {
len = servlen;
Sigprocmask(SIG_UNBLOCK, &sigset_alrm, NULL);
n = recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
Sigprocmask(SIG_BLOCK, &sigset_alrm, NULL);
if (n < 0) {
if (errno == EINTR)
break; /* waited long enough for replies */
else
err_sys("recvfrom error");
} else {
recvline[n] = 0; /* null terminate */
printf("from %s: %s",
Sock_ntop_host(preply_addr, len), recvline);
}
}
}
free(preply_addr);
}
static void
recvfrom_alarm(int signo)
{
return; /* just interrupt the recvfrom() */
}