Python多播接收器中的重复数据包 [英] Duplicate packets in Python multicast receiver

查看:249
本文介绍了Python多播接收器中的重复数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个脚本可以打开套接字并从中读取多播(来自 Python中的多播 )

There is a script that opens a socket and read from it the multicast (from Multicast in Python)

import socket
import struct

MCAST_GRP = '224.1.1.1'
MCAST_PORT = 1234

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

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

while True:
  print sock.recv(10240)

一切都很好,只要我不与另一个多播组的同一脚本并行运行,但是端口是相同的,例如

Everything is fine as long as I do not run parallel to the same script to another multicast group, but the ports are the same, for example

rtp://224.1.1.1:1234
rtp://224.1.1.2:1234

第二个脚本启动后开始混乱-第一个脚本看到第二个和第二个到第一个的数据包.

After starting the second script starts mess - the first script sees packets for the second and the second to first.

我试图做为 mcast.py -相似的结果.

I tried to do as a mcast.py - a similar result.

为什么会发生这种情况以及如何治愈?

Why is this happening and how to cure?

UPD 修复

-sock.bind(('', MCAST_PORT))
+sock.bind((MCAST_GRP, MCAST_PORT))

推荐答案

侦听端口上所有传入连接的应用程序都会将所有消息发送到该端口,无论哪个应用程序发起了多播组成员身份.为了减轻这种情况,请通过将每个应用程序指定为指定给bind的地址tupel中的第一个参数,让每个应用程序侦听其期望数据的多播地址.

An application listening to all incoming connections on a port will get all messages to that port, no matter which application initiated multicast group membership. To mitigate this, have every application listen to the multicast address it's expecting data from, by specifying it as the first argument in the address tupel given to bind.

这篇关于Python多播接收器中的重复数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆