/* Send Multicast Datagram code example: this program creates a UDP socket and configurue the interface as multicast then send 3 message to 126.1.1.1/4321 TO Compile: gcc -g mcastSender.c -o mcastSender */ #include #include #include #include #include #include #include struct in_addr localInterface; struct sockaddr_in groupSock; int sd,i; char databuf[1024] = "Sending a Multicast test message .... !"; int datalen = sizeof(databuf); int main (int argc, char *argv[ ]) { /* Create a datagram socket on which to send. */ sd = socket(PF_INET, SOCK_DGRAM, 0); if(sd < 0) { perror("Opening datagram socket error"); exit(1); } else printf("Opening the datagram socket...OK.\n"); /* Initialize the group sockaddr structure */ memset((void *) &groupSock, 0, sizeof(groupSock)); groupSock.sin_family = AF_INET; groupSock.sin_addr.s_addr = inet_addr("226.1.1.1"); groupSock.sin_port = htons(4321); /* Disable loopback so you do not receive your own datagrams. * * { * * char loopch = 0; * * if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_LOOP, (char *)&loopch, sizeof(loopch)) < 0) * * { * * perror("Setting IP_MULTICAST_LOOP error"); * * close(sd); * * exit(1); * * } * * else * * printf("Disabling the loopback...OK.\n"); * * } * * */ /* Set local interface for outbound multicast datagrams. */ /* The IP address specified must be associated with a local, */ /* multicast capable interface. */ //localInterface.s_addr = inet_addr(INADDR_ANY); localInterface.s_addr = INADDR_ANY; if(setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF, (char *)&localInterface, sizeof(localInterface)) < 0) { perror("Setting local interface error"); exit(1); } else printf("Setting the local interface...OK\n"); /* Send a message to the multicast group specified by the*/ /* groupSock sockaddr structure. */ /*int datalen = 1024;*/ for (i=1; i <4; ++i) if(sendto(sd, databuf, datalen, 0, (struct sockaddr*)&groupSock, sizeof(groupSock)) < 0) {perror("Sending datagram message error");} else printf("Sending datagram message...OK\n"); /* Try the re-read from the socket if the loopback is not disable * * if(read(sd, databuf, datalen) < 0) * * { * * perror("Reading datagram message error\n"); * * close(sd); * * exit(1); * * } * * else * * { * * printf("Reading datagram message from client...OK\n"); * * printf("The message is: %s\n", databuf); * * } * * */ return 0; }