wheybags
wheybags

Reputation: 657

python odd multicast socket exception

I have some example python code that I got from another stack overflow answer (can't remember where), that implements multicasting. The following code should set up a socket object for receiving multicast packets. I encapsulated it in a class like so:

class Multisock:

def __init__(self, MCAST_GRP, MCAST_PORT, packsize):
    import socket
    import struct

    self.MCAST_GRP = MCAST_GRP
    self.MCAST_PORT = MCAST_PORT

    self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self.sock.bind(('', MCAST_PORT))
    mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)

    self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)

However, this gives me this error:

Traceback (most recent call last):
  File "./Audiorecv.py", line 41, in <module>
    sock = MulticastNetworking.Multisock('244.1.1.1', 5007, chunk)
  File "/home/wheybags/Multicast-Voice-Chat/MulticastNetworking.py", line 30, in __init__
    self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
  File "/usr/lib/python2.7/socket.py", line 224, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument

Which is really confusing because if I just set MCAST_GRP statically to a string representing an ip, it works, but it gives the error above if I try to use a constructor argument.

Upvotes: 4

Views: 1698

Answers (1)

javawizard
javawizard

Reputation: 1284

The multicast address you're using, 244.1.1.1, is invalid. Multicast addresses range from 224.0.0.0 to 239.255.255.255. I ran your code with 224.1.1.1, a valid address, and it worked just fine.

Upvotes: 2

Related Questions