简体   繁体   English

侦听组播UDP地址

[英]Listening to a multicast UDP address

I wrote an application in MATLAB to open a UDP socket and listen for incoming datagrams. 我在MATLAB中编写了一个应用程序,用于打开UDP套接字并侦听传入的数据报。 Basically, something like this: 基本上是这样的:

u = udp(rHost, rPort, 'LocalHost', lHost, 'LocalPort', lPort);
u.DatagramAvailableFcn = @(o,e) operateOnData(o,e);
fopen(u);

This works wonderfully when I'm listening to something in a unicast fashion. 当我以单播方式收听内容时,此功能非常有用。 But I would now like to be able to listen to multicast traffic. 但是我现在希望能够收听多播流量。 Apparently, this isn't possible in MATLAB . 显然, 这在MATLAB中是不可能的

The workaround is, per above link, 解决方法是,根据上述链接,

As a workaround to connect to a UDP multicast, you can do the following: 作为连接到UDP多播的替代方法,可以执行以下操作:

  1. Use a Java multicast socket to access it directly from MATLAB. 使用Java多播套接字直接从MATLAB访问它。 For more information, see javadoc or tutorials for the "core java.net" classes from Sun, specifically "java.net.MulticastSocket". 有关更多信息,请参见Sun的“核心java.net”类的javadoc或教程,尤其是“ java.net.MulticastSocket”。 This could be found at: 可以在以下位置找到:

http://java.sun.com/j2se/1.4.2/docs/api/java/net/MulticastSocket.html http://java.sun.com/j2se/1.4.2/docs/api/java/net/MulticastSocket.html

I have no background in Java so this is a struggle for me. 我没有Java的背景,所以对我来说这是一个斗争。 I've only been able to run the following to instantiate a MulticastSocket object: 我只能执行以下操作来实例化MulticastSocket对象:

>> ms = javaObject('java.net.MulticastSocket');

I looked around and found that I also need a java.net.Datagram object to actually contain the incoming stream. 我环顾四周,发现我还需要一个java.net.Datagram对象来实际包含传入的流。

How do I use the MulticastSocket and Datagram objects within the context of MATLAB? 如何在MATLAB上下文中使用MulticastSocketDatagram对象? I'm trying to replicate the functionality of u.DatagramAvailableFcn , ie, fire a callback to operate on the contents of the datagram once I receive one. 我正在尝试复制u.DatagramAvailableFcn的功能,即,一旦收到数据,便触发回调以对数据报的内容进行操作。

EDIT : Looks like this is how I want to go about this in terms of the Java, but now it's getting this back into MATLAB-land... 编辑 :看来这就是我要如何就Java进行此操作,但是现在它又将其返回到MATLAB领域...

I successfully subscribed and received a packet from a multicast stream, by the following: 我通过以下方式成功订阅并接收了来自多播流的数据包:

socket = java.net.MultiSocket(streamPort);
socket.joinGroup(java.net.InetAddress.getByName(streamIP));
socket.setReuseAddress(1);

packet = java.net.DatagramPacket(zeros(1, intmax('uint16'), 'int8'), intmax('uint16'));

socket.receive(packet);

socket.leaveGroup(InetAddress.getByName(streamIP));
socket.close;

msg = packet.getData;
msg = msg(1:packet.getLength);

This was essentially lifted from judp availble on the MathWorks File Exchange. 这实际上是从MathWorks File Exchange上的judp取消的。

I am still looking for a way to get some equivalent of a DatagramReceivedFcn - right now it looks like the socket.receive call is blocking until it times out. 我仍在寻找一种方法来获取与DatagramReceivedFcn等效的方法-现在看起来好像socket.receive调用一直阻塞,直到超时为止。 I can use timer objects to fire the "callback" on a regular basis but that's of course not the same as having a DatagramReceivedFcn . 我可以使用timer对象定期触发“回调”,但这当然与拥有DatagramReceivedFcn

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

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