From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,926e231c0e57abba,start X-Google-Attributes: gidfee84,public X-Google-ArrivalTime: 1994-08-22 03:26:10 PST Path: bga.com!news.sprintlink.net!uunet!usc!elroy.jpl.nasa.gov!decwrl!pa.dec.com!nntpd.lkg.dec.com!mrnews.mro.dec.com!zuunix.zuo.dec.com!pbs From: pbs@zuunix.zuo.dec.com (Philip Shearer @ZUO-DC EBS-Team) Newsgroups: comp.unix.osf.osf1 Subject: [Q] Help with an IP Multicast example. Date: 22 Aug 1994 10:09:33 GMT Organization: Digital Equipment Corporation - Marlboro, MA Message-ID: <339tgt$3et@mrnews.mro.dec.com> NNTP-Posting-Host: zuunix.zuo.dec.com X-Newsreader: Tin 1.1 PL3 /* uname -a == OSF1 macihine_name V2.1 250 alpha The following example of an IP Multicast program was posted to comp.unix.programmer. When I try to run it ( having made changes to the address) on an OSF1 machine . I get an error message on the second setsockopt() function call. "[2]setsockopt: Invalid argument" Any idears why? Also with the first setsockopt(SO_REUSEADDR), (inserting a test on the return), although no error is returned, it does not seem does not seem to alow more than one process per port per machine. Regards Philip ------------------------------------------------------------------------ : From: net@cs.tu-berlin.de (Oliver Laumann) : Newsgroups: comp.unix.programmer : Subject: Re: ANY_MULTICASTING_EXAMPLES_OUT_THERE : Date: 7 Jun 1994 14:13:47 GMT : Organization: Technical University of Berlin, Germany : : > I am looking for some examples of UDP/IP multicasting code. : > : > I need working examples, that use the following line of code. : > : > setsockopt(sockfd, ..., IP_ADD_MEMBERSHIP, ... : : Here is a trivial program that demonstrates IP multicast. I'm posting : it rather than sending it by e-mail, because it's small and other : readers might be interested as well. I have tested it under SunOS 5.3. : : The program first sends a greeting message to the multicast group and : port number defined at the top of the program (224.0.0.0/6060); then it : listens for the greeting messages produced by other instances of the : program and prints everything it receives to standard output. : : Compile the program (don't forget -lsocket -lnsl under SysVR4/SunOS 5), : start it on a few machines (in the same subnet if you aren't running : mrouted), and watch the output. : : Note the call to bind() which is required in addition to joining : the multicast group, and the call to setsockopt() with SO_REUSEADDR : to enable multiple instances of the program to bind to the same : port number. : ------------------------------------------------------------------------- */ #include #include #include #include #define PORT 6060 #define GROUP "224.0.0.0" main() { int s, len, cc, one = 1; static struct sockaddr_in sin; static struct ip_mreq imr; char buf[100]; if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { perror("socket"); return 1; } (void)setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)); sin.sin_family = AF_INET; sin.sin_port = htons(PORT); sin.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) == -1) { perror("bind"); return 1; } /* Send greeting message to multicast group: */ sin.sin_addr.s_addr = htonl(inet_addr(GROUP)); if (sendto(s, "Hi!", 4, 0, (struct sockaddr *)&sin, sizeof(sin)) == -1) { perror("socket"); return 1; } /* Join the group: */ imr.imr_multiaddr = sin.sin_addr; imr.imr_interface.s_addr = htonl(INADDR_ANY); if (setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&imr, sizeof(imr)) == -1) { /* Using setsockopt again with the same options on the * same socket would fail, although it is correct. Therefore: */ if (errno != EADDRINUSE) { perror("[2]setsockopt"); return 1; } } /* Listen for greeting messages sent to the multicast group: */ while (1) { len = sizeof(sin); if ((cc = recvfrom(s, buf, 100, 0, (struct sockaddr *)&sin, &len)) == -1) { perror("recvfrom"); return 1; } printf("%s:\t%.*s\n", inet_ntoa(sin.sin_addr), cc, buf); } /*NOTREACHED*/ }