-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnews_sender_brd.c
More file actions
executable file
·52 lines (46 loc) · 1.23 KB
/
Copy pathnews_sender_brd.c
File metadata and controls
executable file
·52 lines (46 loc) · 1.23 KB
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
//
// Created by Administrator on 2019/1/30.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/fcntl.h>
#define BUF_SIZE 30
void error_handling(char *message);
int main(int argc, char *argv[])
{
int send_sock;
struct sockaddr_in broad_addr;
int so_brd = 1;
FILE *fp;
char buf[BUF_SIZE];
if (argc != 3) {
printf("Usage: %s <GroupID> <PORT>\n", argv[0]);
exit(1);
}
send_sock = socket(PF_INET, SOCK_DGRAM, 0);
memset(&broad_addr, 0, sizeof(broad_addr));
broad_addr.sin_family = AF_INET;
broad_addr.sin_addr.s_addr = inet_addr(argv[1]);
broad_addr.sin_port = htons(atoi(argv[2]));
setsockopt(send_sock, SOL_SOCKET, SO_BROADCAST, (void *)&so_brd, sizeof(so_brd));
if ((fp = fopen("news.txt", "r")) == NULL) {
error_handling("fopen() error");
}
while (!feof(fp)) {
fgets(buf, BUF_SIZE, fp);
sendto(send_sock, buf, strlen(buf), 0, (struct sockaddr *)&broad_addr, sizeof(broad_addr));
sleep(2);
}
fclose(fp);
close(send_sock);
return 0;
}
void error_handling(char *message) {
fputs(message, stderr);
fputc('\n', stderr);
exit(1);
}