Provides an example Server / Client written in MATLAB that utilises the ability to call Java to perform message communication using TCP/IP.
Similar to my previous example (http://www.mathworks.com/matlabcentral/fileexchange/21131) but allows for much more efficient data transmission by using a helper Java class to minimise function call overhead.
To use the example:
- Start 2 instances of MATLAB
- in the first execute the following:
message = char(mod(1:1000, 255)+1);
server(message, 3000, 10)
- in the second execute the following:
javaaddpath('<path to where the .class file is located>');
data = client('localhost', 3000)
For more details see:
Hi Jacobo, I have used these functions to read 16-channel 96kHz sampled 16-bit audio before without issue.
If you are attempting to read the data 1000 times per second then you may have a problem. Read more data less ofter to avoid function call overhead. For example, I read the acoustic data in 0.25 second chunks (24000 samples at a time).
Use the MATLAB profiler tool to see where your code is spending its time. Also, is it using high CPU load or high network load at this time? The issue could be in a number of places.
I used these functions and work well. They are very easy to adapt. But I'm having a problem receiving data fast enough (~100 Double @ 1000Hz) ther is anyway to solve this?
Bi-directional coms can be done by a single socket, although you still need a 'server' and a 'client'. Someone needs to wait for a connection, and the other needs to initiate the connection.
When you get the Java Socket() object, you can get both an InputDataStream and an OutputDataStream (or even a DataStream) and then read/write with the relevant stream.
In order to have bi-directional communication I have to create 2 Sockets? or is there any other option (sockets are bi-directional by default)?
I read 'addpath' instead of 'javaaddpath'.
Thanks for the answer
Don't forget to add the directory containing DataReader.class to the java path:
>> javaaddpath('<path to where the .class file is located>');
(as mentioned above and on the blog page).
The server manage to get a connection with the client but I'm having an error in:
data_reader = DataReader(d_input_stream);
Undefined function 'DataReader' for input arguments of type 'java.io.DataInputStream'.
Excellent! Makes a world of difference when handling string > 1 MB. I use Kevin Bartlett's "TCP/IP Communications in Matlab" (http://www.mathworks.se/matlabcentral/fileexchange/24524-tcpip-communications-in-matlab) which can optionally utilize your java classes and it's lizard fast!