I was playing around with multicast on 9front the other week and managed to get something working (for some definition of working). I have attached 2 test programs for sending and receiving multicast datagrams. Unfortunately, it seems you can't send/receive messages from the same host. I'm not sure what's missing to get that part working.
Also, note that the dial in mcastrecv.c is just a hack to get the local address via getnetconninfo, which doesn't seem to work given the announce directory. Hopefully these are of some use. Would be interested to hear how to fix the mentioned (and unmentioned) shortcomings. -- Cheers, Alex Musolino
#include <u.h> #include <libc.h> static void usage(void) { print("usage: %s mcast-ip port\n", argv0); exits("usage"); } void main(int argc, char **argv) { NetConnInfo *ncinfo; char adir[40], ldir[40], buf[1024]; int fd, acfd, lcfd, dfd, n; ARGBEGIN{ default: usage(); }ARGEND; if(argc != 2) usage(); fd = dial(netmkaddr(argv[0], "udp", argv[1]), 0, 0, 0); ncinfo = getnetconninfo(nil, fd); if(ncinfo == nil) sysfatal("getnetconninfo: %r"); close(fd); acfd = announce(netmkaddr(argv[0], "udp", argv[1]), adir); if(acfd < 0) sysfatal("announce: %r"); if(fprint(acfd, "addmulti %s %s", ncinfo->lsys, argv[0]) < 0) sysfatal("addmulti: %r"); freenetconninfo(ncinfo); for(;;){ lcfd = listen(adir, ldir); if(lcfd < 0) sysfatal("listen: %r"); switch(fork()){ case -1: sysfatal("fork: %r"); case 0: dfd = accept(lcfd, ldir); if(dfd < 0) sysfatal("accept: %r"); if((n = read(dfd, buf, sizeof(buf))) < 0) sysfatal("read: %r"); write(1, buf, n); exits(0); default: close(lcfd); } } }
#include <u.h> #include <libc.h> static void usage(void) { print("usage: %s mcast-addr port\n", argv0); exits("usage"); } void main(int argc, char **argv) { NetConnInfo *ncinfo; char buf[1024]; int fd, cfd, n; ARGBEGIN{ default: usage(); }ARGEND; if(argc != 2) usage(); fd = dial(netmkaddr(argv[0], "udp", argv[1]), 0, 0, &cfd); if(fd < 0) sysfatal("dial: %r"); ncinfo = getnetconninfo(nil, fd); if(ncinfo == nil) sysfatal("getnetconninfo: %r"); if(fprint(cfd, "addmulti %s %s", ncinfo->lsys, argv[0]) < 0) sysfatal("addmulti: %r"); freenetconninfo(ncinfo); while((n = read(0, buf, sizeof(buf))) > 0) write(fd, buf, n); close(fd); }