setting a socket to multicast

Brad Hards bhards at bigpond.net.au
Mon Nov 25 05:46:51 EST 2002


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

G'day,

I'm trying to write a simple UDP server that listens to a multicast
group. This is roughly what I have so far:

<code>
import socket

port = 5353
addr = '224.0.0.251'

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))

print "Waiting on port:", port
while 1:
    data,addr = s.recvfrom(1024)
    print "Received", data, "from", addr
</code>

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')

I'm completely mystified by this. The man page for setsockopt(2) doesn't even mention
EINVAL (at least on my system), and my usual network reference (Stevens, Unix Network
Programming) failed me on this occasion. The documentation refers the man page, which
is nice except that the system call has five arguments and the python call only has three

I did fine one example that was pretty close:
* http://www.pythonapocrypha.com/Chapter15/Chapter15.shtml, which does:
s.setsockopt(SOL_IP,IP_ADD_MEMBERSHIP, inet_aton(addr)+inet_aton(''))
and although the third argument looked a bit silly, it was all I had to work from.

Can anyone enlighten me?

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

iD8DBQE94f+bW6pHgIdAuOMRAtWqAKCAAiAqs0nLkMiohyqrJ7sWC4l4qgCfZppm
uE5k2r4JXerPXtsog2pUU1Y=
=VMh7
-----END PGP SIGNATURE-----





More information about the Python-list mailing list