Skip to content

Instantly share code, notes, and snippets.

@ifukazoo
Created March 30, 2014 12:02
Show Gist options
  • Save ifukazoo/9871838 to your computer and use it in GitHub Desktop.
Save ifukazoo/9871838 to your computer and use it in GitHub Desktop.
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];
socklen_t len;
struct sockaddr *preply_addr;
preply_addr = Malloc(servlen);
Setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on));
Signal(SIGALRM, recvfrom_alarm);
while (Fgets(sendline, MAXLINE, fp) != NULL) {
Sendto(sockfd, sendline, strlen(sendline), 0, pservaddr, servlen);
#if 0
alarm(5);
for ( ; ; ) {
len = servlen;
n = recvfrom(sockfd, recvline, MAXLINE, 0, preply_addr, &len);
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, servlen), recvline);
}
}
#endif
}
}
static void
recvfrom_alarm(int signo)
{
return; /* just interrupt the recvfrom() */
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment