This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket and threading: udp multicast setsockopt fails
Type: behavior Stage: resolved
Components: Extension Modules, Windows Versions: Python 3.1, Python 3.2, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, abgrover, neologix
Priority: normal Keywords:

Created on 2006-03-31 22:16 by abgrover, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
multicastbug.py abgrover, 2006-04-05 12:55 script to reproduce
multicastbug.py abgrover, 2006-04-05 12:58 script to reproduce
Messages (4)
msg28006 - (view) Author: Alan G (abgrover) Date: 2006-03-31 22:16
Using setsockopt to join a multicast group for a udp
socket fails on windows (works on linux).

Test reproduction:
run the attached file

expected behavior:
The snd thread sends three udp packets to the multicast
address.  The rcv thread reads three udp packets.

observed behavior on linux:
the expected behavior

observed behavior on windows:
Exception in thread rcv:
Traceback (most recent call last):
  File "C:\Python24\lib\threading.py", line 442, in
__bootstrap
    self.run()
  File "C:\Python24\lib\threading.py", line 422, in run
    self.__target(*self.__args, **self.__kwargs)
  File
"E:\svn\TTC\trunk\agrover\src\SmcTestCase\multicastbug.py",
line 12, in rcv
    s.setsockopt(SOL_IP, IP_ADD_MEMBERSHIP, sopt)
  File "<string>", line 1, in setsockopt
error: (10022, 'Invalid argument')
msg28007 - (view) Author: Alan G (abgrover) Date: 2006-04-05 12:57
Logged In: YES 
user_id=1098341

More details of config:

Windows used is XP Pro SP2
Linuxen used include Fedora Core and Mandrake
msg114648 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-22 08:54
I can't reproduce this on Windows Vista can someone please confirm my findings.
msg117155 - (view) Author: Charles-François Natali (neologix) * (Python committer) Date: 2010-09-22 21:17
I tested it on a Windows XP box, and encountered the same problem.
The error is raised because Windows XP requires the socket to be bound before calling setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq).
So calling bind() before setsockopt() solves this error.
But then you also need to specify for the sender the interface to use using setsockopt(IPPROTO_IP, IP_MULTICAST_IF, address)

Here's a working example:

---- sender ----
from socket import *

s = socket(AF_INET, SOCK_DGRAM)
s.setsockopt(IPPROTO_IP, IP_MULTICAST_IF, inet_aton('127.0.0.1'))
s.sendto(b'foo', ('224.0.0.1', 4242))
----------------

--- receiver ---
from socket import *

s = socket(AF_INET, SOCK_DGRAM)
s.bind(('127.0.0.1', 4242))
mreq = inet_aton('224.0.0.1') + inet_aton('127.0.0.1')
s.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, mreq)
s.recv(100)
----------------

So it's not a Python bug.
Since multicast is tricky, it might be a good idea to add a short example somewhere in the documentation though.
History
Date User Action Args
2022-04-11 14:56:16adminsetgithub: 43134
2011-08-30 18:34:35neologixsetstatus: open -> closed
resolution: not a bug
stage: needs patch -> resolved
2010-09-22 21:17:19neologixsetnosy: + neologix
messages: + msg117155
2010-08-22 08:54:58BreamoreBoysetnosy: + BreamoreBoy

messages: + msg114648
versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 3.0
2009-03-21 02:03:07ajaksu2settitle: udp multicast setsockopt fails -> socket and threading: udp multicast setsockopt fails
stage: needs patch
type: behavior
components: + Extension Modules
versions: + Python 2.6, Python 3.0, - Python 2.4
2006-03-31 22:16:42abgrovercreate