-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path12.MULTICAST_UDP_SERVER.cpp
More file actions
55 lines (43 loc) · 1.5 KB
/
12.MULTICAST_UDP_SERVER.cpp
File metadata and controls
55 lines (43 loc) · 1.5 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
53
54
55
// MULTICAST UDP SERVER
#include <iostream>
#include <sys/socket.h>
#include <sys/types.j>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define MULTICAST_GROUP "239.0.0.1" // Class D Multicast address
int main() {
int sock_fd;
struct sockaddr_in servaddr;
char str[100];
// 1. Create a UDP socket
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (sock_fd < 0) {
printf("Socket creation failed");
exit(0);
}
// 2. Initialize the multicast address structure
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(12345);
servaddr.sin_addr.s_addr = inet_addr(MULTICAST_GROUP);
// 3. Set IP_MULTICAST_LOOP option to control loopback
int loopback = 1;
setsockopt(sock_fd, IPPROTO_IP, IP_MULTICAST_LOOP, &loopback, sizeof(loopback))
// 4. Set IP_MULTICAST_IF option to define the local interface
struct in_addr local_interface;
local_interface.s_addr = htonl(INADDR_ANY); // Use default interface
setsockopt(sock_fd, IPPROTO_IP, IP_MULTICAST_IF, &local_interface, sizeof(local_interface))
// 5. Send multicast messages
while (1) {
fgets(str, sizeof(message)),stdin;
if (strcmp(str, "exit") == 0) {
break;
}
sendto(sock_fd, str, strlen(str), 0, (struct sockaddr*)&servaddr, sizeof(servaddr))
}
close(sock_fd);
return 0;
}