setting a socket to multicast

Adam Langley usenet at imperialviolet.org
Mon Nov 25 07:26:39 EST 2002


On Mon, 25 Nov 2002 21:46:51 +1100, Brad Hards wrote:

> s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
> s.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, socket.inet_aton(addr))
> s.bind(("", port))

> However when I run it, it bombs out:
> Traceback (most recent call last):
>   File "udpserver.py", line 8, in ?
>     s.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, 
>                  socket.inet_aton(addr))
> socket.error: (22, 'Invalid argument')

>From the ip(7) manpage:
	IP_ADD_MEMBERSHIP: Join  a  multicast  group.  Argument  is  a  struct
              ip_mreqn structure.

I don't think the Python setsockopt function is smart enough to see the
IP_ADD_MEMBERSHIP option and build the struct. I suggest you look into
   http://www.python.org/doc/current/lib/module-struct.html
and build a struct ip_mreqn

> I'm completely mystified by this. The man page for setsockopt(2) doesn't
> even mention EINVAL (at least on my system)

It is documented in the ip(7) manpage.

> except that the system call has five arguments and the python call only
> has three

because the first argument (the socket) comes from the object and the last
argument's value (data length) is implicit from the data being passed.





More information about the Python-list mailing list