From: Daemon Le Date: 2004-05-21T23:33:49+09:00 Subject: Re: Multicasting with Ruby? Thanks Dan! It worked beautifully. Changing "host" to "addr" (OK, that was dumb on my part), getting rid of the connect, and doing IP_ADD_MEMBERSHIP after bind fixed all my current woes. Here's with your corrections as it runs on the Redhat Linux box... --- 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::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 ) --- end ------------------------------------------ Thanks again, Daemon Dan Janowski wrote in news:B1BAF9E9-AAE5-11D8-AC4D- 00039357B54C@3skel.com: > 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 ) >