# Winxp multicast socket reading problem

**URL:** <https://www.ruby-forum.com/t/winxp-multicast-socket-reading-problem/114058>\
**Category:** Ruby\
**Created:** [September 7, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/winxp-multicast-socket-reading-problem/114058 "2007-09-07T12:51:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![akifusenet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@akifusenet](https://www.ruby-forum.com/u/akifusenet)\
**Post date:** [September 7, 2007, 12:51pm UTC](https://www.ruby-forum.com/t/winxp-multicast-socket-reading-problem/114058/1 "2007-09-07T12:51:49Z")

</div>

Hi

I am trying to read some data from a multicast address. So I googled and  
found this example.

require ‘socket’  
require ‘ipaddr’

MULTICAST\_ADDR = “225.4.5.6”  
PORT = 5000

ip = IPAddr.new(MULTICAST\_ADDR).hton + IPAddr.new(“0.0.0.0”).hton

sock = UDPSocket.new  
sock.setsockopt(Socket::IPPROTO\_IP, Socket::IP\_ADD\_MEMBERSHIP, ip)  
sock.bind(Socket::INADDR\_ANY, PORT)

loop do  
msg, info = sock.recvfrom(1024)  
puts “MSG: #{msg} from #{info[2]} (#{info[3]})/#{info[1]} len  
#{msg.size}”  
end

It turned out that one-click installer for WinXp(ruby 1.8.6 (2007-03-13  
patchlevel 0) [i386-mswin32]) has a bug on Socket::Constants and  
Socket::IP\_ADD\_MEMBERSHIP is not defined.

As explained in  
[http://rubyforge.org/tracker/?func=detail&group\_id=167&aid=9438&atid=715](http://rubyforge.org/tracker/?func=detail&group_id=167&aid=9438&atid=715)

I added this line  
Socket::IP\_ADD\_MEMBERSHIP = 12 unless  
socket.const\_defined?(‘IP\_ADD\_MEMBERSHIP’)  
to the top of my code.

But it didnt work again. Now it says  
error on setsockopt:Invalid argument(EINVAL)

What could be the problem?

Thanks in advance.

Akif,

---

<div class="post-metadata">

**Author:** ![akifusenet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@akifusenet](https://www.ruby-forum.com/u/akifusenet)\
**Post date:** [September 7, 2007, 1:25pm UTC](https://www.ruby-forum.com/t/winxp-multicast-socket-reading-problem/114058/2 "2007-09-07T13:25:27Z")

</div>

I tried the same code in Linux. It doesnt give any error. So it has to  
be with Windows. Anybody knows how to deal with this?

Thanks.

Akif,

---

<div class="post-metadata">

**Author:** ![akifusenet](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e95f7d/32.png) [@akifusenet](https://www.ruby-forum.com/u/akifusenet)\
**Post date:** [December 7, 2007, 9:38pm UTC](https://www.ruby-forum.com/t/winxp-multicast-socket-reading-problem/114058/3 "2007-12-07T21:38:18Z")

</div>

I ran into this same problem and finally discovered that in Windows you  
have to call bind before setsockopt. This gets rid of the  
“setsockopt:Invalid argument(EINVAL)” message.

Eric R.

Akif T. wrote:

> Hi
> 
> I am trying to read some data from a multicast address. So I googled and  
> found this example.
> 
> require ‘socket’  
> require ‘ipaddr’
> 
> MULTICAST\_ADDR = “225.4.5.6”  
> PORT = 5000
> 
> ip = IPAddr.new(MULTICAST\_ADDR).hton + IPAddr.new(“0.0.0.0”).hton
> 
> sock = UDPSocket.new  
> sock.setsockopt(Socket::IPPROTO\_IP, Socket::IP\_ADD\_MEMBERSHIP, ip)  
> sock.bind(Socket::INADDR\_ANY, PORT)
> 
> loop do  
> msg, info = sock.recvfrom(1024)  
> puts “MSG: #{msg} from #{info[2]} (#{info[3]})/#{info[1]} len  
> #{msg.size}”  
> end
> 
> But it didnt work again. Now it says  
> error on setsockopt:Invalid argument(EINVAL)
> 
> What could be the problem?
> 
> Thanks in advance.
> 
> Akif,
