-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews_sender_brd.c
49 lines (44 loc) · 1.08 KB
/
news_sender_brd.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#define BUF_SIZE 30
void error_handling(char *buf)
{
fputs(buf, stderr);
fputc('\n',stderr);
exit(1);
}
int main(int argc, char const *argv[])
{
int send_sock;
struct sockaddr_in broad_adr;
FILE *fp;
char buf[BUF_SIZE];
int so_brd = 1;
if (argc != 3)
{
printf("Usage : %s <GroupIP> <port>\n", argv[0]);
exit(1);
}
send_sock = socket(PF_INET, SOCK_DGRAM, 0);
memset(&broad_adr, 0, sizeof(broad_adr));
broad_adr.sin_family = AF_INET;
if(!inet_aton(argv[1], &broad_adr.sin_addr))
error_handling("convertion error"); // multicast IP
broad_adr.sin_port = htons(atoi(argv[2])); //multicast Port
setsockopt(send_sock, SOL_SOCKET, SO_BROADCAST, (void*)&so_brd, sizeof(so_brd));
if((fp = fopen("news.txt","r")) == NULL)
error_handling("open file error");
while(!feof(fp))
{
fgets(buf, BUF_SIZE, fp);
sendto(send_sock, buf, strlen(buf), 0, (struct sockaddr*)&broad_adr, sizeof(broad_adr));
sleep(2);
}
fclose(fp);
close(send_sock);
return 0;
}