Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongoose
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
esp
mongoose
Commits
04012760
Commit
04012760
authored
Sep 23, 2016
by
Marko Mikulicic
Committed by
Cesanta Bot
Sep 23, 2016
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add small multicast server demo for mongoose
PUBLISHED_FROM=c5f6b0c64bc1e43030743b6aae5d06ae231df5dc
parent
0ec1a141
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
0 deletions
+110
-0
Makefile
examples/mcast_udp_server/Makefile
+10
-0
mcast_udp_server.c
examples/mcast_udp_server/mcast_udp_server.c
+100
-0
No files found.
examples/mcast_udp_server/Makefile
0 → 100644
View file @
04012760
PROG
=
mcast_udp_server
ifeq
($(OS),Windows_NT)
all
:
@
echo
"This example is not yet supported on
$(OS)
"
else
include
../examples.mk
endif
clean
:
examples/mcast_udp_server/mcast_udp_server.c
0 → 100644
View file @
04012760
// Copyright (c) 2016 Cesanta Software Limited
// All rights reserved
//
// This software is dual-licensed: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2 as
// published by the Free Software Foundation. For the terms of this
// license, see <http://www.gnu.org/licenses/>.
//
// You are free to use this software under the terms of the GNU General
// Public License, but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// Alternatively, you can license this software under a commercial
// license, as set out in <https://www.cesanta.com/license>.
//
// This demo shows how to configure a mongoose UDP connection to receive
// multicast traffic.
#include <netinet/in.h>
#include "mongoose.h"
#define MCAST_GROUP "224.0.22.1"
#define DEFAULT_PORT "1234"
static
void
ev_handler
(
struct
mg_connection
*
nc
,
int
ev
,
void
*
p
)
{
struct
mbuf
*
io
=
&
nc
->
recv_mbuf
;
(
void
)
p
;
switch
(
ev
)
{
case
MG_EV_RECV
:
printf
(
"Received (%zu bytes): '%.*s'
\n
"
,
io
->
len
,
(
int
)
io
->
len
,
io
->
buf
);
mbuf_remove
(
io
,
io
->
len
);
nc
->
flags
|=
MG_F_SEND_AND_CLOSE
;
break
;
default:
break
;
}
}
void
usage
(
char
**
argv
)
{
fprintf
(
stderr
,
"%s: -i address_of_mcast_interface [ -g %s ] [ -p %s ]
\n
"
,
argv
[
0
],
MCAST_GROUP
,
DEFAULT_PORT
);
exit
(
1
);
}
int
main
(
int
argc
,
char
**
argv
)
{
struct
mg_mgr
mgr
;
const
char
*
port
=
"1234"
;
struct
mg_connection
*
nc
;
struct
ip_mreq
group
;
const
char
*
interface
=
NULL
;
const
char
*
mcast_group
=
MCAST_GROUP
;
int
i
;
/* Parse command line arguments */
for
(
i
=
1
;
i
<
argc
;
i
++
)
{
// IP address of the interface where to join a multicast group.
if
(
strcmp
(
argv
[
i
],
"-i"
)
==
0
)
{
interface
=
argv
[
++
i
];
}
else
if
(
strcmp
(
argv
[
i
],
"-g"
)
==
0
)
{
mcast_group
=
argv
[
++
i
];
}
else
if
(
strcmp
(
argv
[
i
],
"-p"
)
==
0
)
{
port
=
argv
[
++
i
];
}
}
if
(
interface
==
NULL
)
{
usage
(
argv
);
}
mg_mgr_init
(
&
mgr
,
NULL
);
{
char
listen
[
256
];
sprintf
(
listen
,
"udp://%s"
,
port
);
nc
=
mg_bind
(
&
mgr
,
listen
,
ev_handler
);
}
if
(
nc
==
NULL
)
{
perror
(
"cannot bind
\n
"
);
exit
(
1
);
}
group
.
imr_multiaddr
.
s_addr
=
inet_addr
(
mcast_group
);
group
.
imr_interface
.
s_addr
=
inet_addr
(
interface
);
if
(
setsockopt
(
nc
->
sock
,
IPPROTO_IP
,
IP_ADD_MEMBERSHIP
,
(
char
*
)
&
group
,
sizeof
(
group
))
<
0
)
{
perror
(
"Adding multicast group error"
);
exit
(
1
);
}
printf
(
"Starting mcast server on port %s listening to group %s
\n
"
,
port
,
mcast_group
);
for
(;;)
{
mg_mgr_poll
(
&
mgr
,
1000
);
}
mg_mgr_free
(
&
mgr
);
return
0
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment