From: Dan Janowski Date: 2004-05-21T14:14:30+09:00 Subject: Re: Multicasting with Ruby? I got your program running under solaris. Solaris has some alignment padding in the sockaddr struct, so the [4..7] is needed. I added address reuse, though on *BSD it should be SO_REUSEPORT which is not a ruby constant; for Solaris SO_REUSEADDR works. The only thing I materially changed was to remove the connect and put the add_membership after the bind. Hope this works for you. Dan require 'socket' port = 1212 addr = '228.5.6.8' host = Socket.gethostname maddr = addr.split('.').collect! { |b| b.to_i }.pack('CCCC') # mreq from netinet/in.h # in_addr multicast address # in_addr local IP address of interface sa=(Socket.gethostbyname(host)[3])[4..7] mreq = maddr + sa sock = UDPSocket.new sock.setsockopt(Socket::SOL_SOCKET,Socket::SO_REUSEADDR,1) sock.bind(addr, port) sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, mreq) # Check send ... sock.send('Hello', 0,addr,port) sock.send('World', 0,addr,port) # Check listen ... count=0 5.times { count += 1 p "COUNT = #{count}" p sock.recvfrom(8) } sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_DROP_MEMBERSHIP, mreq) exit( 0 ) On May 20, 2004, at 03:43, Daemon Le wrote: > Hi, > > > I'm trying to "listen" on a multicast using Ruby. After trolling the > news groups and "google", I was able to find a rudimentary set of code > in > my attempt to listen on multicasting, but it doesn't quite work and I > don't know why? Does anyone know of good resources (webpage or book) > for > multicasting with Ruby? Any help would be appreciated. > > Here's the sample code that "semi" works... > > --- begin ------------------------------------ > require 'socket' > > port = 1212 > addr = '228.5.6.8' > host = Socket.gethostname > maddr = addr.split('.').collect! { |b| b.to_i }.pack('CCCC') > mreq = maddr + Socket.gethostbyname(host)[3] > sock = UDPSocket.new > > sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, mreq) > sock.bind(host, port) > sock.connect(host, port) > > # Check send ... > sock.send('Hello', 0, addr, port) > sock.send('World', 0, addr, port) > > # Check listen ... > count=0 > 5.times { > count += 1 > p "COUNT = #{count}" > p sock.recvfrom(8) > } > > sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_DROP_MEMBERSHIP, mreq) > > exit( 0 ) > --- end -------------------------------------- > > The "Check send" works just fine to send into the multicast address. I > check this while listening with tcpdump (i.e., tcpdump 'ip multicast > and > src port 1212 and host 228.5.6.8'). But I'm unable to pick up on the > "Check listen" part. It just basically hangs with "COUNT = 1" and > waits > forever even though I can clearly see packets being transmitted over > the > multicast address in question. > > FYI. I'm starting to look at the "rb_spread" module, but I would > rather > try to do this without first... > > > - Daemon >