Description: non blocking tcpip event based sockets for Matlab.
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, OP_WRITE, OP_ACCEPT, OP_CONNECT). Leaves the command prompt available for you to keep working while it runs in the background. 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.
% open a connection to something, set blocking to false
% see the channelConnect() mfile for more details.
clientChannel = channelConnect(hostName, portNum, soRcvBuffSize, soSndBuffSize, tcpNoDelay, blocking);
% The default behavior of the non-blocking sockets is to fire
% a read event as soon as any data is read. This results in
% data segmentation that must be handled in Matlab. If you
% intialize the SocketManager with a byteOffset (zero indexed)
% to the location of an int32 that gives the size of the message
% the bytes will be collected until the full message is received
% and then it will be sent. (Generally this would be a value
% included in the data header).
% In the case below it expects the int32
% at bytes 4-7 (zero indexed) to tell the total message size.
socketManager = SocketManager(4);
% registers clientChannel OP_READ events
% register a Matlab callback for read events
% echoServerTestBuffer generates
request = echoServerTestBuffer();
len = channelWrite(clientChannel, request);
% onReceive will fire whenever data is waiting to
% be read on clientChannel
For a complete example see "main.m" in the attached files.
The echo server constructor can take one argument which is a
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:
You then MUST restart Matlab.
The class files were built on my Windows 32 bit machine against Java 1.5.0_07.
This submission was inspired by my job and enabled by the useful information on UndocumentedMatlab.com. Particularly this post:
I hope to flesh this out a little more soon,make it a little more user friendly, and add some nice documentation. The functionality for OP_ACCEPT for server sockets is not fully implemented yet although the callback is visible. Do not use it.
Related: waiting, Complete, onreceive, channelwriteclientchannel, Generates, Request, mainm
O/S:BSD, Linux, Solaris, Mac OS X
File Size: 10.0 KB