setting a socket to multicast

Brad Hards bhards at bigpond.net.au
Mon Nov 25 16:16:47 EST 2002


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Mon, 25 Nov 2002 23:26, Adam Langley wrote:
<snip>
> 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
Thanks - makes sense, but I'm not there yet. I think I'm creating the 
structure properly, but still no joy:

<code>
#This from Python Cookbook, recipe 10.5
#Credits: Alex Martelli, Greg Jorgenen
def dottedQuadtoNum(ip):
    "convert decimal dotted quad string to long integer"
    return struct.unpack('>L', socket.inet_aton(ip))[0]

port = 5353
addr = '224.0.0.251'

ip_mreqn = struct.pack('IIi', dottedQuadtoNum(addr), 0, 0)
print struct.unpack('IIi', ip_mreqn)

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, ip_mreqn)
</code>

Which produces:
(3758096635L, 0L, 0)
Traceback (most recent call last):
  File "udpserver.py", line 18, in ?
    s.setsockopt(socket.SOL_IP, socket.IP_ADD_MEMBERSHIP, ip_mreqn)
socket.error: (22, 'Invalid argument')

More ideas?

Brad
- -- 
http://linux.conf.au. 22-25Jan2003. Perth, Aust. I'm registered. Are you?
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE94pNAW6pHgIdAuOMRArjMAJ9xCCIz9K6fva7CnD/9qfEeH/Tk2ACgsBvJ
BS6SdayFiUrqy3lC4wNoB+o=
=rDsc
-----END PGP SIGNATURE-----





More information about the Python-list mailing list