help with multicast read (broken?)

Sean McNeil sean at mcneil.com
Sun Oct 10 15:56:00 PDT 2004


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: This is a digitally signed message part
Url : http://lists.freebsd.org/pipermail/freebsd-current/attachments/20041010/f979267e/attachment.bin


More information about the freebsd-current mailing list