Run Java Code in Matlab

Hello Everyone,
I want to run the following Java Code in Matlab to open a UDP socket and listen for incoming multicast datagrams. ------------------------------------------------------------------------------------------------------------
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);
------------------------------------------------------------------------------------------------------------
I am trying to do the same in matlab but i get error: Undefined variable java or class java.net.MultiSocket. How to add javaclasspath?
Regards

7 Comments

PTP
PTP on 8 Mar 2016
Hello Guys,
Could someone please answer this. I am struggling to find the real solution.
regards
PTP - this looks to be the same code posted at http://stackoverflow.com/questions/21734856/listening-to-a-multicast-udp-address. Have you tried contacting the author of this code?
Hey Guys,
I am able to do now but I just need the bytes received to convert into something meaningfull such as String/Characters so that Humans can read it. As of now I receive some binary data as such.
public static void main(String args[]) {
socket.receive(dgram);
System.err.println("Received " + dgram.getLength() + " bytes from " + dgram.getAddress());
dgram.setLength(b.length);
String received = new String(dgram.getData());
System.out.println("Bytes received and printed at Console: " + received);
Could you please tell me how to convert that "received" variable into Character/String?
Kind Regards
PTP - please clarify how the new code is related to your MATLAB program.
PTP
PTP on 9 Mar 2016
Edited: PTP on 9 Mar 2016
Hi Geoff,
I am working on a Java wrapper in matlab. Since Matlab cannot handle mutlicast transmission with respect to UDP so I had to run the Java code in matlab.
By setting classpath.txt and initializing the class object and then finally calling the main function of the class in java, I am able to receive the data from another node. But now I am successfull in doing that also, the only problem now that remains is that in the end I am unable to read the data received since it is some random binary characters.
How to convert that data into String/Character so that Humans can interpret it.
I would appreciate your help.
PTP - what happens when you create the received String variable? Are you expecting something else (a valid string) or ...? What data type is dgram? Does dgram.GetData() return an array of bytes?
I am unable to read the data received since it is some random binary characters. Should it be non-random instead?

Sign in to comment.

Answers (0)

Categories

Asked:

PTP
on 5 Mar 2016

Commented:

on 9 Mar 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!