/* NAME: Multicast sender SYNOPSIS: McastSend DESCRIPTION: The program creates a datagram socket in the inet domain. It sends data to a multicast group and gets an echo of the message. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include "packetReg.h" int main( int argc, char * argv[] ) { int s; struct sockaddr_in mc_group; struct sockaddr_in addr; int rc; /* linux */ unsigned long int GroupIP; /* Solaris in_addr_t GroupIP; */ char loop, ttl; Packet pkt; if (argc != 4) { fprintf(stderr, "Usage : %s \n",argv[0]); exit(0); } if( (GroupIP = inet_addr(argv[2]))== -1) printf("error in inet_addr\n"); strcpy(pkt.name, argv[1]); mc_group.sin_family = AF_INET; mc_group.sin_port = htons(atoi(argv[3])); mc_group.sin_addr.s_addr = GroupIP; s = socket (PF_INET,SOCK_DGRAM,0); /* By default, messages sent to the multicast group are looped back to the local host. this function disables that. loop = 1 means enable loopback loop = 0 means disable loopback NOTE : by default, loopback is enabled */ loop = 1; if( setsockopt(s,IPPROTO_IP,IP_MULTICAST_LOOP,(char *) &loop, sizeof(u_char)) == -1 ) { printf("error in setting loopback\n"); } /* set the Time-To-Live value */ ttl = 5; if( setsockopt(s,IPPROTO_IP,IP_MULTICAST_TTL,(char *) &ttl, sizeof(u_char)) == -1 ) { printf("error in setting loopback value\n"); } for(;;) { printf("\nType anything followed by RETURN, or type CTRL-D to exit\n"); rc=read(0,pkt.msg, BUFSIZE); if (rc == 0) break; pkt.msg[rc-1]='\0'; pkt.length = sizeof(pkt)-BUFSIZE+rc; printf("Sending %d bytes \"%s\"\n", pkt.length, pkt.msg); if (sendto(s, (char *)&pkt, pkt.length, 0, (struct sockaddr*)&mc_group, sizeof(mc_group)) <0 ) perror("sending datagram message"); } printf ("EOF... exit\n"); close(s); }