-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path14_broadcasting.c
194 lines (162 loc) · 5.11 KB
/
14_broadcasting.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/fcntl.h>
#include <netdb.h>
#include <errno.h>
#include <sys/poll.h>
#include <pthread.h>
#define PROTOCOL "udp"
#define BROADCAST_ADDR "255.255.255.255"
#define BROADCAST_PORT 5555
#define MESSAGE_SIZE 1024
struct broadcast_t
{
int fd;
struct sockaddr_in addr_receiver;
socklen_t addr_receiver_len;
};
void print_usage(const char *program_name)
{
fprintf(stderr, "Usage: %s <your_nickname>\n", program_name);
}
void report_error(const char* message)
{
fprintf(stderr, "Error: %s\n", message);
}
int setup_broadcast_receiver(struct broadcast_t* receiver_info)
{
int rc;
receiver_info->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (receiver_info->fd < 0)
{
report_error("socket() failed for receiver");
return -1;
}
int optval = 1;
rc = setsockopt(receiver_info->fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
if (rc != 0)
{
report_error("setsockopt(SO_REUSEADDR) failed");
return -1;
}
receiver_info->addr_receiver.sin_family = AF_INET;
receiver_info->addr_receiver.sin_port = htons(BROADCAST_PORT);
receiver_info->addr_receiver.sin_addr.s_addr = htonl(INADDR_ANY);
receiver_info->addr_receiver_len = sizeof(receiver_info->addr_receiver);
rc = bind(receiver_info->fd, (struct sockaddr *)&receiver_info->addr_receiver, receiver_info->addr_receiver_len);
if (rc < 0)
{
report_error("bind() failed for receiver");
return -1;
}
return 0;
}
int setup_broadcast_sender(struct broadcast_t* sender_info)
{
int rc;
sender_info->fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sender_info->fd < 0)
{
report_error("socket() failed for sender");
return -1;
}
int optval = 1;
rc = setsockopt(sender_info->fd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
if (rc != 0)
{
report_error("setsockopt(SO_BROADCAST) failed");
return -1;
}
sender_info->addr_receiver.sin_family = AF_INET;
sender_info->addr_receiver.sin_port = htons(BROADCAST_PORT);
inet_pton(AF_INET, BROADCAST_ADDR, &sender_info->addr_receiver.sin_addr);
sender_info->addr_receiver_len = sizeof(sender_info->addr_receiver);
return 0;
}
void* broadcast_receiver_thread_func(void* arg)
{
struct broadcast_t* broadcast_receiver_info = (struct broadcast_t*)calloc(1, sizeof(struct broadcast_t));
if (setup_broadcast_receiver(broadcast_receiver_info) != 0)
{
report_error("setup_broadcast_receiver() failed");
return NULL;
}
char buffer[MESSAGE_SIZE];
printf("Start to listen broadcast messages\n");
while (1)
{
memset(buffer, 0, MESSAGE_SIZE);
int received_bytes = recvfrom(broadcast_receiver_info->fd, buffer, MESSAGE_SIZE, 0, (struct sockaddr*)&broadcast_receiver_info->addr_receiver, &broadcast_receiver_info->addr_receiver_len);
if (received_bytes <= 0)
{
report_error("Broadcast receiver recvfrom() failed");
}
else
{
printf("Received broadcast message: %s\n", buffer);
}
}
close(broadcast_receiver_info->fd);
free(broadcast_receiver_info);
return NULL;
}
void* broadcast_sender_thread_func(void* arg)
{
char* nick_name = (char*)arg;
struct broadcast_t* broadcast_sender_info = (struct broadcast_t*)calloc(1, sizeof(struct broadcast_t));
if (setup_broadcast_sender(broadcast_sender_info) != 0)
{
report_error("setup_broadcast_sender() failed");
return NULL;
}
char broadcast_message[MESSAGE_SIZE];
while (1)
{
memset(broadcast_message, 0, MESSAGE_SIZE);
sprintf(broadcast_message, "%s is active", nick_name);
int sent_bytes = sendto(broadcast_sender_info->fd, broadcast_message, MESSAGE_SIZE, 0, (struct sockaddr*)&broadcast_sender_info->addr_receiver, broadcast_sender_info->addr_receiver_len);
if (sent_bytes <= 0)
{
report_error("Send broadcast message failed");
}
sleep(1);
}
close(broadcast_sender_info->fd);
free(broadcast_sender_info);
return NULL;
}
int main(int argc, char** argv)
{
if (argc != 2)
{
print_usage(argv[0]);
return -1;
}
pthread_t broadcast_receiver_thread;
pthread_create(&broadcast_receiver_thread, NULL, broadcast_receiver_thread_func, NULL);
pthread_detach(broadcast_receiver_thread);
pthread_t broadcast_sender_thread;
pthread_create(&broadcast_sender_thread, NULL, broadcast_sender_thread_func, argv[1]);
pthread_detach(broadcast_sender_thread);
char buffer_stdin[MESSAGE_SIZE];
while (1)
{
printf("User input: ");
memset(buffer_stdin, 0, MESSAGE_SIZE);
fgets(buffer_stdin, MESSAGE_SIZE, stdin);
buffer_stdin[strcspn(buffer_stdin, "\r\n")] = 0;
if (strcmp(buffer_stdin, "exit") == 0
|| strcmp(buffer_stdin, "quit") == 0)
{
break;
}
}
return 0;
}