Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: pbs@zuunix.zuo.dec.com (Philip Shearer @ZUO-DC EBS-Team)
Subject: [Q] Help with an IP Multicast example.
Date: 22 Aug 1994 10:09:33 GMT	[thread overview]
Message-ID: <339tgt$3et@mrnews.mro.dec.com> (raw)

/*
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 <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#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*/
}



             reply	other threads:[~1994-08-22 10:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-08-22 10:09 Philip Shearer @ZUO-DC EBS-Team [this message]
1994-08-23 13:44 ` [Q] Help with an IP Multicast example Philip Shearer @ZUO-DC EBS-Team
1994-08-24 14:31   ` Philip Shearer @ZUO-DC EBS-Team
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox