File Exchange

image thumbnail

matSock

version 1.3.0.0 (31.7 KB) by Jude Collins
non blocking tcpip event based sockets for Matlab.

4 Downloads

Updated 28 Aug 2012

View License

A small java class that implements a channel multiplexor in a separate thread and allows you to register socket connections and Matlab callback functions for socket channel events (OP_READ and OP_ACCEPT). Leaves the command prompt available for you to keep working while it runs in the background firing Matlab callbacks to process the data. The SocketChannel that causes the event is passed into the Matlab callback in the event data. There is no need to open multiple instances of Matlab to test it. I have included an echo server for testing.

I get data rates >100Mbps.

Usage:
For a complete example see "main.m" and "demoServer.m" in the attached files.

Installation:
You will need to edit your "classpath.txt" (type 'edit classpath.txt' at the matlab command prompt) and add the path to the bin directory that contains the java classes from the zip file.

It should look something like this when you are done:
...
glnxa64=$matlabroot/java/jarext/jogl.a64.jar
$matlabroot/java/jarext/jogl.jar
C:/MatSock/bin/
mac=/System/Library/Java
maci=/System/Library/Java

You then MUST restart Matlab.

The class files were built on my Windows 32 bit machine against Java 1.5.0_07.

I have included the source code so that you can compile the code yourself on your own system if you desire. I suppose that this is necessary if you are running 64 bit.

This submission was inspired by my job and enabled by the useful information on UndocumentedMatlab.com. Particularly this post:
http://undocumentedmatlab.com/blog/matlab-callbacks-for-java-events/

There is still a lot of work to be done to make it more configurable and implement all of the network options but what is there seems to run well.
Try it out and let me know what additional features would be helpful.... and leave me a comment to let me know what you think.

Cite As

Jude Collins (2020). matSock (https://www.mathworks.com/matlabcentral/fileexchange/35608-matsock), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (10)

Zach Trotz

Hi Jude,

How does one actually read the data written to a server, using this code?

Dear Jude

Thanks for this remarkable piece of code. I was able to run it without much trouble.

as I put your library in a subfolder of my software I added to my server booting

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%CHECK IF MATSOCK LIBRARY PATH HAS BEEN ADDED %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CHK= exist('echoServerTestBuffer.m');
if CHK==0
disp('Adding MATSOCK library');
cd matSock_20120828
CDIR=pwd;
addpath(CDIR);
cd ..
else
disp('MATSOCK library......[OK]');
end

which add the library in the path if needed

I digging a bit in the code, it seems that when the data are sent to the server the dimension of the packet must be always specified in bytes 5-8 of the packet in BIG ENDIAN format (I guess this originates the error reported by cK). Honestly I do not have that familiarity with the tcp handshake, however, the tcp server included in the Matlab toolbox (which is definitely quite less flexible), allow me to transfer without specifically formatting the packet.

Could you comment about this specific choice? (and sorry if my question turns out to be trivial)

Best

Marco

cr

Hi Jude,
I have been trying to make this work in a specific fashion but seem to be missing something fundamental. I'm trying to write 10 bytes to server from a client (socket created on another Matlab session) but dont understand how to read is using channelRead(). Do I have to suppress @onReceive callback ? I cannot seem to find what should go as input socket when I use channelRead(). Where do I find the appropriate socket number?

Your demo file works fine, but when I replace echoServerTestBuffer() with some data like int8(1:10) there seems to be an exception "thread(SocketManager4): 'SocketManager:run:readSizeError(1566465888)' -> you should probably close this channel"
although onReceive function receives all 10bytes. Can you please clarify?
Thanks.

Thanks for the 5 stars but I do not think it is to that level yet, but I am glad that you like it. I have slowly been adding more features in and reorganizing the code. I hope to update it before too long. And it all still needs to be documented.

Bryan

Jude Collins

Ren, You need to compile against the Java 6 sdk not Java 7.
"Java 1.6.0_17-b04" is known externally as "6u17" where the "u" stands for update.
Any Java 6 sdk version after 6u17 will probably work.
The latest Java 6 sdk release is 6u34.
http://www.oracle.com/technetwork/java/javase/downloads/index.html

JavaSE stands for "Java Standard Edition".

You are correct that serverBind() is an m-file that got left out last time I updated this project. I will upload that by tomorrow.

Poorren

Hi Jude,
I really like your code and your method. However, I'm C/C++ guys, not familiar with Java.

The version of Java libs on my PC (Windows 7 64bit) is as follows.
Java SE Development Kit 7 Update 6 (64bit) 1.7.0.60
Java 7 Update 6 (64bit) 7.0.60
Java 7 Update 6 7.0.60

The matlab version info is as below.

>> version

ans =

7.13.0.564 (R2011b)

>> version -Java

ans =

Java 1.6.0_17-b04 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode

Previously, I installed older version of JRE. I uninstalled all of them, re-install JRE/JDK as above.
The IDEI used is tide.jar. ( http://sourceforge.net/projects/tide/ )

Unfortunately, using the self compiled libraries, I persistently got the " Warning: A Java exception occurred trying to load the com/jude/nio/EchoServer class: ".

As an attempt of luck, I directly use the library you provided in the package. Everything works now!!
I already could run main.m, and are trying to connect to instrument for further experiment.

One more question is the demoServer.m. Is that un-finished work?
I attempt to run that script, and couldn't find serverBind(hostName, portNum) even I import the libraries.

Anyway, thanks for your code and excellent work! Hope to see more perfect Matsock!

Nice day!
Jeff

Jude Collins

Ren, your methods seems correct.
http://www.java.net/node/664117

Make sure you are compiling against a jdk that is compatible with the version of Matlab that you are using.

What version of Matlab are you running?
Are you using an IDE like netbeans or eclipse?

Poorren

Hello, Jude,
I compile the Java source on my Windows7. Then, tried to run main.m. And found the following exception. I'm not farmilar with Java. After googling this exception, I found this could lead to version mismatch between JDK and JRE. I reinstall the JRE and JDK on my machine. The exception still there. Could you provide more help on this? Thanks advance!

###############################
12 echoServer = EchoServer(echoPorts); %the default port is 10059
Warning: A Java exception occurred trying to load the com/jude/nio/EchoServer class:
Java exception occurred:
java.lang.UnsupportedClassVersionError: com/jude/nio/EchoServer : Unsupported major.minor version 51.0

at java.lang.ClassLoader.defineClass1(Native Method)

at java.lang.ClassLoader.defineClass(Unknown Source)

at java.security.SecureClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.defineClass(Unknown Source)

at java.net.URLClassLoader.access$000(Unknown Source)

at java.net.URLClassLoader$1.run(Unknown Source)

at java.security.AccessController.doPrivileged(Native Method)

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClassInternal(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at com.mathworks.jmi.OpaqueJavaInterface.findClass(OpaqueJavaInterface.java:470)

Jude Collins

I should submit an updated version in the next few days that supports handling OP_ACCEPT callbacks for server sockets also, and includes some other enhancements. Will someone that downloads this let me know how it works for you? thanks.

Updates

1.3.0.0

Added in the serverBind() m-file that was left out last time I uploaded it. Made a few other tweaks.

1.2.0.0

Included the java source code so that you can build the class yourself.

1.1.0.0

Implemented the OP_ACCEPT callback for server sockets.
Expanded the "main.m" demo file.
Added a "demoServer.m".
msgSizePosition indicator is registered per socket.

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux