Multicast Server/Listener
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close */
#define SERVER_PORT 1500
#define MAX_MSG 100
int main(int argc, char *argv[]) {
int sd, rc, n, cliLen;
struct ip_mreq mreq;
struct sockaddr_in cliAddr, servAddr;
struct in_addr mcastAddr;
struct hostent *h;
char msg[MAX_MSG];
if(argc!=2) {
printf("usage : %s <mcast address>\n",argv[0]);
exit(0);
}
/* get mcast address to listen to */
h=gethostbyname(argv[1]);
if(h==NULL) {
printf("%s : unknown group '%s'\n",argv[0],argv[1]);
exit(1);
}
memcpy(&mcastAddr, h->h_addr_list[0],h->h_length);
/* check given address is multicast */
if(!IN_MULTICAST(ntohl(mcastAddr.s_addr))) {
printf("%s : given address '%s' is not multicast\n",argv[0],
inet_ntoa(mcastAddr));
exit(1);
}
/* create UDP socket */
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) {
printf("%s : cannot create socket\n",argv[0]);
exit(1);
}
/* bind port */
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
servAddr.sin_port=htons(SERVER_PORT);
if(bind(sd,(struct sockaddr *) &servAddr,sizeof(servAddr))<0)
{
printf("%s : cannot bind port %d \n",argv[0],SERVER_PORT);
exit(1);
}
/* join multicast group */
mreq.imr_multiaddr.s_addr=mcastAddr.s_addr;
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
rc = setsockopt(sd,IPPROTO_IP,IP_ADD_MEMBERSHIP,
(void *) &mreq, sizeof(mreq));
if(rc<0) {
printf("%s : cannot join multicast group '%s'",argv[0],
inet_ntoa(mcastAddr));
exit(1);
}
else {
printf("%s : listening to mgroup %s:%d\n",
argv[0],inet_ntoa(mcastAddr), SERVER_PORT);
/* infinite server loop */
while(1) {
cliLen=sizeof(cliAddr);
n = recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr *) &cliAddr,&cliLen);
if(n<n) {
printf("%s : cannot receive data\n",argv[0]);
continue;
}
printf("%s : from %s:%d on %s : %s\n",argv[0],
inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),
argv[1],
msg);
}/* end of infinite server loop */
}
return 0;
}
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close */
#define SERVER_PORT 1500
#define MAX_MSG 100
int main(int argc, char *argv[]) {
int sd, rc, i;
unsigned char ttl = 1;
struct sockaddr_in cliAddr, servAddr;
struct hostent *h;
if(argc<3) {
printf("usage %s <mgroup> <data1> <data2> ... <dataN>\n",argv[0]);
exit(1);
}
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s : unknown host '%s'\n",argv[0],argv[2]);
exit(1);
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0],h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
/* check if dest address is multicast */
if(!IN_MULTICAST(ntohl(servAddr.sin_addr.s_addr))) {
printf("%s : given address '%s' is not multicast \n",argv[0],
inet_ntoa(servAddr.sin_addr));
exit(1);
}
/* create socket */
sd = socket(AF_INET,SOCK_DGRAM,0);
if (sd<0) {
printf("%s : cannot open socket\n",argv[0]);
exit(1);
}
/* bind any port number */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
if(bind(sd,(struct sockaddr *) &cliAddr,sizeof(cliAddr))<0) {
perror("bind");
exit(1);
}
if(setsockopt(sd,IPPROTO_IP,IP_MULTICAST_TTL, &ttl,sizeof(ttl))<0) {
printf("%s : cannot set ttl = %d \n",argv[0],ttl);
exit(1);
}
printf("%s : sending data on multicast group '%s' (%s)\n",argv[0],
h->h_name,inet_ntoa(*(struct in_addr *) h->h_addr_list[0]));
/* send data */
for(i=2;i<argc;i++) {
rc = sendto(sd,argv[i],strlen(argv[i])+1,0,
(struct sockaddr *) &servAddr, sizeof(servAddr));
if (rc<0) {
printf("%s : cannot send data %d\n",argv[0],i-1);
close(sd);
exit(1);
}
}/* end for */
/* close socket and exit */
close(sd);
exit(0);
}
$ ./mcastserver 224.0.0.200
./mcastserver : listening to mgroup 224.0.0.200:1500
./mcastserver : from 192.168.10.52:51654 on 224.0.0.200 : hello
./mcastserver : from 192.168.10.52:51654 on 224.0.0.200 : bye
$ ./mcastclient 224.0.0.200 hello bye
./mcastclient : sending data on multicast group '224.0.0.200' (224.0.0.200)
The switch periodically sends Query (Type 0x11) messages to broadcast address 224.0.0.1
When a server joins a new multicast group it sends a Membership report (Type 0x16) to all the hosts.
When a server leaves the group it sends a Leave Group (0x17) message to broadcast address 224.0.0.2
Also read:
http://ntrg.cs.tcd.ie/undergrad/4ba2/multicast/antony/
http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/Multicast-HOWTO.html
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close */
#define SERVER_PORT 1500
#define MAX_MSG 100
int main(int argc, char *argv[]) {
int sd, rc, n, cliLen;
struct ip_mreq mreq;
struct sockaddr_in cliAddr, servAddr;
struct in_addr mcastAddr;
struct hostent *h;
char msg[MAX_MSG];
if(argc!=2) {
printf("usage : %s <mcast address>\n",argv[0]);
exit(0);
}
/* get mcast address to listen to */
h=gethostbyname(argv[1]);
if(h==NULL) {
printf("%s : unknown group '%s'\n",argv[0],argv[1]);
exit(1);
}
memcpy(&mcastAddr, h->h_addr_list[0],h->h_length);
/* check given address is multicast */
if(!IN_MULTICAST(ntohl(mcastAddr.s_addr))) {
printf("%s : given address '%s' is not multicast\n",argv[0],
inet_ntoa(mcastAddr));
exit(1);
}
/* create UDP socket */
sd = socket(AF_INET,SOCK_DGRAM,0);
if(sd<0) {
printf("%s : cannot create socket\n",argv[0]);
exit(1);
}
/* bind port */
servAddr.sin_family=AF_INET;
servAddr.sin_addr.s_addr=htonl(INADDR_ANY);
servAddr.sin_port=htons(SERVER_PORT);
if(bind(sd,(struct sockaddr *) &servAddr,sizeof(servAddr))<0)
{
printf("%s : cannot bind port %d \n",argv[0],SERVER_PORT);
exit(1);
}
/* join multicast group */
mreq.imr_multiaddr.s_addr=mcastAddr.s_addr;
mreq.imr_interface.s_addr=htonl(INADDR_ANY);
rc = setsockopt(sd,IPPROTO_IP,IP_ADD_MEMBERSHIP,
(void *) &mreq, sizeof(mreq));
if(rc<0) {
printf("%s : cannot join multicast group '%s'",argv[0],
inet_ntoa(mcastAddr));
exit(1);
}
else {
printf("%s : listening to mgroup %s:%d\n",
argv[0],inet_ntoa(mcastAddr), SERVER_PORT);
/* infinite server loop */
while(1) {
cliLen=sizeof(cliAddr);
n = recvfrom(sd,msg,MAX_MSG,0,(struct sockaddr *) &cliAddr,&cliLen);
if(n<n) {
printf("%s : cannot receive data\n",argv[0]);
continue;
}
printf("%s : from %s:%d on %s : %s\n",argv[0],
inet_ntoa(cliAddr.sin_addr),ntohs(cliAddr.sin_port),
argv[1],
msg);
}/* end of infinite server loop */
}
return 0;
}
Multicast Client/Sender
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> /* close */
#define SERVER_PORT 1500
#define MAX_MSG 100
int main(int argc, char *argv[]) {
int sd, rc, i;
unsigned char ttl = 1;
struct sockaddr_in cliAddr, servAddr;
struct hostent *h;
if(argc<3) {
printf("usage %s <mgroup> <data1> <data2> ... <dataN>\n",argv[0]);
exit(1);
}
h = gethostbyname(argv[1]);
if(h==NULL) {
printf("%s : unknown host '%s'\n",argv[0],argv[2]);
exit(1);
}
servAddr.sin_family = h->h_addrtype;
memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0],h->h_length);
servAddr.sin_port = htons(SERVER_PORT);
/* check if dest address is multicast */
if(!IN_MULTICAST(ntohl(servAddr.sin_addr.s_addr))) {
printf("%s : given address '%s' is not multicast \n",argv[0],
inet_ntoa(servAddr.sin_addr));
exit(1);
}
/* create socket */
sd = socket(AF_INET,SOCK_DGRAM,0);
if (sd<0) {
printf("%s : cannot open socket\n",argv[0]);
exit(1);
}
/* bind any port number */
cliAddr.sin_family = AF_INET;
cliAddr.sin_addr.s_addr = htonl(INADDR_ANY);
cliAddr.sin_port = htons(0);
if(bind(sd,(struct sockaddr *) &cliAddr,sizeof(cliAddr))<0) {
perror("bind");
exit(1);
}
if(setsockopt(sd,IPPROTO_IP,IP_MULTICAST_TTL, &ttl,sizeof(ttl))<0) {
printf("%s : cannot set ttl = %d \n",argv[0],ttl);
exit(1);
}
printf("%s : sending data on multicast group '%s' (%s)\n",argv[0],
h->h_name,inet_ntoa(*(struct in_addr *) h->h_addr_list[0]));
/* send data */
for(i=2;i<argc;i++) {
rc = sendto(sd,argv[i],strlen(argv[i])+1,0,
(struct sockaddr *) &servAddr, sizeof(servAddr));
if (rc<0) {
printf("%s : cannot send data %d\n",argv[0],i-1);
close(sd);
exit(1);
}
}/* end for */
/* close socket and exit */
close(sd);
exit(0);
}
$ ./mcastserver 224.0.0.200
./mcastserver : listening to mgroup 224.0.0.200:1500
./mcastserver : from 192.168.10.52:51654 on 224.0.0.200 : hello
./mcastserver : from 192.168.10.52:51654 on 224.0.0.200 : bye
$ ./mcastclient 224.0.0.200 hello bye
./mcastclient : sending data on multicast group '224.0.0.200' (224.0.0.200)
The switch periodically sends Query (Type 0x11) messages to broadcast address 224.0.0.1
When a server joins a new multicast group it sends a Membership report (Type 0x16) to all the hosts.
When a server leaves the group it sends a Leave Group (0x17) message to broadcast address 224.0.0.2
Also read:
http://ntrg.cs.tcd.ie/undergrad/4ba2/multicast/antony/
http://www.ibiblio.org/pub/Linux/docs/HOWTO/other-formats/html_single/Multicast-HOWTO.html
No comments:
Post a Comment