Вопрос

I wrote an application in MATLAB to open a UDP socket and listen for incoming datagrams. 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.

The workaround is, per above link,

As a workaround to connect to a UDP multicast, you can do the following:

  1. Use a Java multicast socket to access it directly from MATLAB. For more information, see javadoc or tutorials for the "core java.net" classes from Sun, specifically "java.net.MulticastSocket". This could be found at:

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. I've only been able to run the following to instantiate a MulticastSocket object:

>> 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.

How do I use the MulticastSocket and Datagram objects within the context of MATLAB? I'm trying to replicate the functionality of u.DatagramAvailableFcn, i.e., fire a callback to operate on the contents of the datagram once I receive one.

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...

Это было полезно?

Решение

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.

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. 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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top