Skip to content

Instantly share code, notes, and snippets.

@francesco-romano
Created August 8, 2016 15:56
Show Gist options
  • Save francesco-romano/5a1634aae430046b58757ea7920db16f to your computer and use it in GitHub Desktop.
Save francesco-romano/5a1634aae430046b58757ea7920db16f to your computer and use it in GitHub Desktop.
Multicast example
/*
* listener.c -- joins a multicast group and echoes all data it receives from
* the group to its stdout...
*
* Antony Courtney, 25/11/94
* Modified by: Fr�d�ric Bastien (25/03/04)
* to compile without warning and work correctly
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <ace/SOCK_Dgram_Mcast.h>
#include <ace/INET_Addr.h>
#define HELLO_PORT 12345
#define HELLO_GROUP "225.0.0.37"
#define MSGBUFSIZE 256
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd, nbytes;
socklen_t addrlen;
struct ip_mreq mreq;
char msgbuf[MSGBUFSIZE];
u_int yes=1;
#ifndef ACE_SOCKETS
printf("Receiver using BSD sockets\n");
/* create what looks like an ordinary UDP socket */
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
return -1;
}
/* allow multiple sockets to use the same PORT number */
if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(yes)) < 0) {
perror("Reusing ADDR failed");
return -1;
}
#else
printf("Receiver using ACE\n");
#endif
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=htonl(INADDR_ANY);
addr.sin_port=htons(HELLO_PORT);
#ifndef ACE_SOCKETS
/* bind to receive address */
if (bind(fd,(struct sockaddr *) &addr,sizeof(addr)) < 0) {
perror("bind");
return -1;
}
/* use setsockopt() to request that the kernel join a multicast group */
mreq.imr_multiaddr.s_addr=inet_addr(HELLO_GROUP);
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
if (setsockopt(fd,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq)) < 0) {
perror("setsockopt");
return -1;
}
#else
ACE_INET_Addr aceAddr(&addr, sizeof(addr));
ACE_SOCK_Dgram_Mcast socket;
socket.join(aceAddr);
#endif
/* now just enter a read-print loop */
while (1) {
addrlen=sizeof(addr);
#ifndef ACE_SOCKETS
if ((nbytes=recvfrom(fd,(void*)msgbuf,MSGBUFSIZE,0,
(struct sockaddr *) &addr,&addrlen)) < 0) {
#else
if (socket.recv((void*)msgbuf, MSGBUFSIZE, aceAddr) < 0) {
#endif
perror("recvfrom");
return -1;
}
puts(msgbuf);
}
}
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <ace/SOCK_Dgram_Mcast.h>
#include <ace/INET_Addr.h>
#define HELLO_PORT 12345
#define HELLO_GROUP "225.0.0.37"
int main(int argc, char *argv[])
{
struct sockaddr_in addr;
int fd, cnt;
struct ip_mreq mreq;
char *message="Hello, World!";
/* set up destination address */
memset(&addr,0,sizeof(addr));
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr(HELLO_GROUP);
addr.sin_port=htons(HELLO_PORT);
printf("ciao: %d\n", ACE_SOCK_Dgram_Mcast::options::DEFOPTS);
#ifndef ACE_SOCKETS
printf("Sender using BSD Sockets\n");
/* create what looks like an ordinary UDP socket */
if ((fd=socket(AF_INET,SOCK_DGRAM,0)) < 0) {
perror("socket");
exit(1);
}
#else
printf("Sender using ACE\n");
ACE_INET_Addr multicastAddress(&addr, sizeof(addr));
ACE_SOCK_Dgram socket;//(ACE_SOCK_Dgram_Mcast::options::OPT_BINDADDR_NO);
if (socket.open(multicastAddress) < 0) {
perror("Could not open socket");
}
#endif
/* now just sendto() our destination! */
while (1) {
#ifndef ACE_SOCKETS
if (sendto(fd,message,sizeof(message),0,(struct sockaddr *) &addr,
sizeof(addr)) < 0) {
#else
if (socket.send(message, sizeof(message),multicastAddress) <= 0) {
#endif
perror("sendto");
exit(1);
}
sleep(1);
}
}
cmake_minimum_required(VERSION 3.0)
project(ACE_Mcast)
include_directories(/usr/local/Cellar/ace/6.3.3/include)
add_executable(ACEsender ACEsender.c++)
target_link_libraries(ACEsender /usr/local/Cellar/ace/6.3.3/lib/libACE.dylib)
option(USE_ACE_SOCKETS "Use ACE sockets" YES)
if (USE_ACE_SOCKETS)
message("Using ACE sockets")
add_definitions("-DACE_SOCKETS")
endif()
add_executable(ACEreceiver ACEreceiver.c++)
target_link_libraries(ACEreceiver /usr/local/Cellar/ace/6.3.3/lib/libACE.dylib)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment