简体   繁体   English

扭曲的Python:多播服务器无法正常工作

[英]Twisted Python: multicast server not working as expected

I am experimenting with twisted python's multicast protocol. 我正在尝试使用扭曲的python的多播协议。 This is a simple example: 这是一个简单的示例:

I created two servers, listening on 224.0.0.1 and 224.0.0.2 like below: 我创建了两个服务器,分别在224.0.0.1和224.0.0.2上进行监听,如下所示:

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from twisted.application.internet import MulticastServer

class MulticastServerUDP(DatagramProtocol):
    def __init__ (self, group, name):
        self.group = group
        self.name = name
    def startProtocol(self):
        print '%s Started Listening' % self.group
        # Join a specific multicast group, which is the IP we will respond to
        self.transport.joinGroup(self.group)

    def datagramReceived(self, datagram, address):
        print "%s Received:"%self.name + repr(datagram) + repr(address)


reactor.listenMulticast(10222, MulticastServerUDP('224.0.0.1', 'SERVER1'), listenMultiple = True)
reactor.listenMulticast(10222, MulticastServerUDP('224.0.0.1', 'SERVER2'), listenMultiple = True)

reactor.run()

Then I run this code to send "HELLO": 然后,我运行以下代码发送“ HELLO”:

import socket

MCAST_GRP = '224.0.0.1'
MCAST_PORT = 10222

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, 2)
sock.sendto("HELLO", (MCAST_GRP, MCAST_PORT))

The results were quite confusing. 结果相当混乱。 There are several cases: 有几种情况:

-When I set all group IP and MCAST_GRP to 224.0.0.1, both servers received the message (expected) -When I set both servers' group IP to 224.0.0.1 and MCAST_GRP in the sending script to 224.0.0.2 (or something different from 224.0.0.1), both servers did not receive the message (expected) -当我将所有组IP和MCAST_GRP设置为224.0.0.1时,两台服务器均收到消息(预期)-当我将两台服务器的组IP设置为224.0.0.1且发送脚本中的MCAST_GRP设置为224.0.0.2时(或与224.0.0.1),两个服务器均未收到消息(预期)

-When I set one server's group IP to 224.0.0.1 and the other 224.0.0.2, strange things happen. -当我将一台服务器的组IP设置为224.0.0.1并将另一台服务器的IP设置为224.0.0.2时,会发生奇怪的事情。 When I set MCAST_GRP to 224.0.0.1 or 224.0.0.2, I expected only ONE of the two servers to receive the message. 当我将MCAST_GRP设置为224.0.0.1或224.0.0.2时,我期望两台服务器中只有一台可以接收到该消息。 The result was that BOTH servers received the message. 结果是两个服务器都收到了该消息。 I am not sure what is going on. 我不确定发生了什么。 Can someone explain this? 有人可以解释吗?

Note: I am running these on the same machine. 注意:我在同一台计算机上运行这些文件。

SL SL

It's a little tricky, indeed. 确实有点棘手。 You must write it this way: 您必须这样写:

reactor.listenMulticast(
    10222, 
    MulticastServerUDP('224.0.0.1', 'SERVER1'),
    listenMultiple=True,
    interface='224.0.0.1'
)

reactor.listenMulticast(
    10222, 
    MulticastServerUDP('224.0.0.2', 'SERVER2'),
    listenMultiple=True,
    interface='224.0.0.2'
)

I had the same problem before. 我之前也遇到过同样的问题。 I had to look at the source to find it out. 我必须查看源才能找到答案。 But I solved it, because of my background in network programming in C. 但是我解决了这个问题,因为我有使用C语言进行网络编程的背景。

Multicast is wacky and platform (Linux, Windows, OS X, etc) implementations of multicast are even wackier. 组播是古怪的,并且组播(Linux,Windows,OS X等)的平台实现甚至更古怪。

Twisted is just reflecting the platform's multicast behavior here, so this is only vaguely a Twisted-related question. Twisted在这里只是反映了平台的多播行为,因此这只是一个与Twisted相关的问题。 Really, it's a multicast question and a platform question. 确实,这是一个多播问题和一个平台问题。

Here's a slightly educated guess as to what's going on. 关于发生的事情,这是一个稍微受过教育的猜测。

Multicast works by having hosts subscribe to addresses. 组播通过让主机订阅地址来工作。 When a program running on the host joins a group (eg 224.0.0.1), the host makes a note of this locally and does some network operations (IGMP) to tell nearby hosts (probably via a router, but I'm fuzzy on the details of this part) that it is now interested in messages for that group. 当主机上运行的程序加入一个组(例如224.0.0.1)时,主机会在本地进行记录,并执行一些网络操作(IGMP)告诉附近的主机(可能是通过路由器,但我对此很模糊)该部分的详细信息),现在它对该组的消息感兴趣。

In the ideal universe of the creators of multicast, that subscription propagates all the way through the internet. 在多播创建者的理想世界中,该订阅一直通过互联网传播。 This is necessary so that whenever anyone anywhere on the internet sends a message to that group, whichever routers get their hands on it can deliver it to all the hosts that have subscribed to the group. 这是必要的,这样,只要Internet上任何地方的任何人向该组发送邮件,无论哪个路由器将其传递给已订阅该组的所有主机。 This is supposed to be more efficient than broadcast because only hosts that have subscribed need the message delivered to them. 这应该比广播更有效,因为只有已订阅的主机才需要将消息传递给它们。 Since routers are tracking subscriptions, they can skip sending the traffic down links that have no subscribed hosts. 由于路由器正在跟踪订阅,因此它们可以跳过没有订阅主机的链接发送流量。

In the real universe, multicast subscriptions usually don't get propagated very far (eg, they reach the first router, probably the one running your house LAN, and stop there). 在真实的世界中,多播订阅通常不会传播得很远(例如,它们到达第一个路由器,可能是运行您的家庭局域网的那个路由器,然后停在那儿)。

So it may seem like all that information about the ideal universe is irrelevant to this scenario. 因此,关于理想宇宙的所有信息似乎都与这种情况无关。 However! 然而! My suspicion is that most of the people implementing multicast thought really, really hard about that first part and were pretty tired by the time they were done implementing it. 我的怀疑是,大多数实施多播的人们对第一部分确实非常认真地思考,并且对完成多播的时间感到非常疲倦。

Once a message for a multicast group actually gets to a host, the host needs to deliver it to the programs that are actually interested in it. 一旦组播组的消息实际到达主机,主机就需要将其传递给对它真正感兴趣的程序。 Here, I suspect, implementers were too tired to do the right thing. 我怀疑在这里,实施者太累了,无法做正确的事情。 Instead, they did a variety of lazy, easy things (depending on your platform). 相反,他们做了各种各样的懒惰,简单的事情(取决于您的平台)。 For example, some of them just visited every single open socket on the system that was subscribed to a multicast group and delivered the message to them. 例如,其中一些刚访问了系统上已订阅多播组的每个打开的套接字,并将消息传递给了他们。

On other platforms, you'll sometimes find that a single multicast message is delivered to a single listening multicast socket more than once. 在其他平台上,有时您会发现一条多播消息多次传递到一个侦听多播套接字。 And of course there's the popular issue of multicast messages never being delivered at all. 当然,还有一个普遍存在的组播消息问题,那就是根本无法传递。

Enjoy your wacky times in multicast land! 在多播领域享受您的古怪时代!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM