/* ------------------------ */ /* --- mcast_example.c ---- */ /* ------------------------ */ /* can start as sender or receiver; sender sends 3 messages to each mutlicast group and receiver reads them. To compile: gcc -g mcast_example.c multicast.c -o mcast_example */ #include "multicast.h" int CreateMcastSocket(); void sendMessage(int s); void getMessage(int inSock); void map_mcast_address(char * session_numb); //char GroupIPaddress1[]="226.1.1.1";/* multicast group IP address */ char GroupIPaddress1[]="224.4.4.4";/* multicast group IP address */ char GroupIPaddress2[]="225.5.5.5";/* multicast group IP address */ int UDPport; /* port number */ u_char LOOP=1, TimeToLive=3; /* ttl value */ struct in_addr localInterface; main(int argc,char *argv[]) { /* parse the command line to get the multicast group address */ if (argc < 3) { printf("Usage: %s port_number s|r [0|1]\n",argv[0]); exit(1); } if (argc > 4) LOOP= atoi(argv[3]); UDPport = atoi(argv[1]); /* We need TWO Sockets for Send and Receive (WHY?) */ if (!strcmp(argv[2],"s")) { int CMcastSock=-1; printf("Sending to %s and %s mcast groups...\n",GroupIPaddress1,GroupIPaddress2); if ((CMcastSock = socket(PF_INET,SOCK_DGRAM, 0)) < 0) { printf("can't create socket: %s \n", strerror(errno)); exit(-1); } setTTLvalue(CMcastSock,&TimeToLive); /* setting theTTL value */ setLoopback(CMcastSock,LOOP); /*0=disable,1=enable loopback; default is enable*/ sendMessage(CMcastSock); close(CMcastSock); } else if (!strcmp(argv[2],"r")) { int SMcastSock=-1; printf("Joining %s and %s mcast groups...\n",GroupIPaddress1,GroupIPaddress2); SMcastSock = CreateMcastSocket(UDPport); /* OR SMcastSock = passiveUDP(UDPport); */ joinGroup(SMcastSock, GroupIPaddress1); joinGroup(SMcastSock, GroupIPaddress2); getMessage(SMcastSock); leaveGroup(SMcastSock, GroupIPaddress1); leaveGroup(SMcastSock, GroupIPaddress2); close(SMcastSock); } else { printf("Usage: %s port_number s|r [0|1]\n",argv[0]); exit(1); } } /* This functions: allocates a socket for getting the group multicast messages */ int CreateMcastSocket(int port) { int s,length,err; struct sockaddr_in groupHost; /* multicast group host info structure */ /* Get the multicast group host information */ groupHost.sin_family=AF_INET; groupHost.sin_port=htons(port); /* wildcard address: means it may receive any unicast or multicast pkts destined to this port */ groupHost.sin_addr.s_addr = htonl(INADDR_ANY); /*unicast ot mcast packets*/ /* OR (this is the only part missing in passiveUDP())==> groupHost.sin_addr.s_addr = htonl(inet_addr(GroupIPaddress1)); this way this server is restricted to listen for mcast packets ONLY*/ /* Allocate a UDP socket and set the multicast options */ if ((s = socket(PF_INET,SOCK_DGRAM, 0)) < 0) { printf("can't create socket:%s \n", strerror(errno)); exit(-1); } /* allow multipule processes to bind to same multicast port */ reusePort(s); /* bind the UDP socket to the mcast address to recv messages from the group */ if((bind(s,(struct sockaddr *) &groupHost, sizeof(groupHost))== -1)) { printf("error in bind\n"); exit(2); } return s; } /* This function reads the input from STDIN and sends it to the group */ void sendMessage(int sockfd) { char sendBuf[]="This is a Multicast Test Message in 224.4.4.4/4444\n"; char sendBuf1[]="This is a Multicast Test Message in 225.5.5.5/4444\n"; int i,bytes=0; struct sockaddr_in dest; /* send the message to the group */ dest.sin_family = AF_INET; dest.sin_port = htons(UDPport); dest.sin_addr.s_addr = inet_addr(GroupIPaddress1); // Setting the local interface -- new added requirement localInterface.s_addr = INADDR_ANY; if(setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0) { perror("Setting local interface error"); exit(1); } for (i=1; i<=3;i++) { // printf("Msg Sent:%s",sendBuf); dest.sin_addr.s_addr = inet_addr(GroupIPaddress1); if ( sendto(sockfd,sendBuf,strlen(sendBuf),0,(struct sockaddr *) &dest, sizeof(dest)) < 0 ) { printf("error in sendto \n"); exit(-1); } sleep(1); dest.sin_addr.s_addr = inet_addr(GroupIPaddress2); if ( sendto(sockfd,sendBuf1,strlen(sendBuf),0,(struct sockaddr *) &dest, sizeof(dest)) < 0 ) { printf("error in sendto \n"); exit(-1); } } } /* Get the multicast message sent by the group and print out to STDOUT */ void getMessage(int inSock) { int i,bytes=0; char recvBuf[MAX_LEN]; for (i=1; i<=6;i++) { memset(recvBuf,'\0',MAX_LEN); bytes = recv(inSock,recvBuf,MAX_LEN,0); if (bytes < 0) { printf("error in reading from multicast socket\n"); exit(-1); } else if(bytes == 0) printf("zero bytes read\n"); else /* print the message to STDOUT */ { if (write(1,recvBuf,bytes) < 0) { printf("error in write to STDOUT \n"); exit(-1); } } } }