-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmulticast_srv.c
102 lines (90 loc) · 2.55 KB
/
multicast_srv.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
/***************************************************************************
* Copyright (C) 2017 - 2020, Lanka Hsu, <lankahsu@gmail.com>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
***************************************************************************/
#include "utilx9.h"
#define MULTICAST_PORT 3618
#define MULTICAST_IP "239.255.255.250" // 224.0.0.0 ~ 239.255.255.255
int main(int argc, char* argv[])
{
int sockfd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
int reuse = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char*) &reuse, sizeof(reuse));
struct sockaddr_in local_addr;
memset(&local_addr, 0, sizeof(local_addr));
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = htonl(INADDR_ANY);
local_addr.sin_port = htons(MULTICAST_PORT);
printf("listen ... (%s:%d)\n", MULTICAST_IP, MULTICAST_PORT);
if (bind(sockfd, (struct sockaddr *) &local_addr, sizeof(local_addr)) < 0)
{
perror("bind");
return -1;
}
printf("setsockopt IP_ADD_MEMBERSHIP\n");
struct ip_mreq seMember;
memset((unsigned char *)&seMember, 0x00, sizeof(seMember));
seMember.imr_multiaddr.s_addr = inet_addr(MULTICAST_IP);
seMember.imr_interface.s_addr = htonl(INADDR_ANY);
if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &seMember,sizeof(seMember)) < 0)
{
perror("setsockopt");
return -1;
}
#if (1)
while (1)
{
char buff[1024] = "";
struct sockaddr_in cli;
int n = sizeof(cli);
int nread = 0;
fd_set fdrset;
fd_set fdwset;
fd_set fdeset;
FD_ZERO(&fdrset);
FD_SET(sockfd, &fdrset);
FD_ZERO(&fdwset);
FD_SET(sockfd, &fdwset);
FD_ZERO(&fdeset);
FD_SET(sockfd, &fdeset);
//printf("call select ~~~\n");
struct timeval tv;
tv.tv_sec = 1;
tv.tv_usec = 0;
int result = select(sockfd+1, &fdrset, (fd_set *)NULL, &fdeset, &tv);
//printf("(result: %d)\n", result);
if (result == -1)
{
}
else if (result==0)
{
}
else if (FD_ISSET(sockfd, &fdrset))
{
#if (1)
nread = recvfrom(sockfd, buff, 1023, 0, (struct sockaddr *)&cli, (socklen_t*)&n);
if (nread>0)
{
printf("%s - %s\n", inet_ntoa(cli.sin_addr), buff);
}
#endif
}
sleep(1);
}
#endif
if (sockfd>=0)
{
close(sockfd);
}
return 0;
}