help with multicast read (broken?)

From: Sean McNeil <sean_at_mcneil.com>
Date: Sun, 10 Oct 2004 15:55:56 -0700
I just installed 5.3-B7 on an i386 machine.  All went well, but I cannot
get it to read data from a multicast address.  I wrote a program that
works just fine on both x86 and ppc linux platforms.  It does the
following:

    struct sockaddr_in address;
    int reuse_addr = 1;
    int fd, i;
    char *stream = "224.1.1.1";
    char *port = "1234";
    char *buffer;

    buffer = malloc (1500);
    if (buffer == NULL)
    {
	fprintf(stderr, "Cannot allocate buffer space\n");
	return -1;
    }

    memset((char *) &address, 0, sizeof(address));
    address.sin_family = AF_INET;
    address.sin_port = htons(atoi(port));

    fd = socket(PF_INET, SOCK_DGRAM, 0);
    if (fd < 0) {
	fprintf(stderr, "Cannot open socket for stream %s\n", stream);
	return -1;
    }

    setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr,
sizeof(reuse_addr));

    address.sin_addr.s_addr = inet_addr(stream);

    if (bind(fd, (struct sockaddr *) &address, sizeof(address)) < 0) {
	fprintf(stderr, "Cannot bind to stream %s\n", stream);
	close(fd);
	return -1;
    }

    if (IN_MULTICAST(address.sin_addr.s_addr)) {

	struct ip_mreq mreq; 

	mreq.imr_multiaddr.s_addr = address.sin_addr.s_addr;
	mreq.imr_interface.s_addr = htonl(INADDR_ANY);

	if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
		       &mreq, sizeof(mreq)) < 0) {
	    fprintf(stderr, "Cannot join multicast for %s\n", stream);
	    close(fd);
	    return -1;
	}
    }

    i = recvfrom(fd, buffer, 1500, 0, NULL, NULL);


recvfrom never returns.  I've looked at my program with sockstat and it
has 224.1.1.1:1234 open for foreign address *:*.  Anyone see a problem
here?

I've placed my complete program at

www.mcneil.com/~sean/freebsd/stream.c

you can compile simply as 

cc -o stream stream.c

I would appreciate comments/suggestions.  Again, works fine under
various cpus with Linux.

Sean


Received on Sun Oct 10 2004 - 20:56:00 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:38:16 UTC