Thread Subject: Interfacing MATLAB and Flash with TCP/IP

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Akhil Prem

Date: 30 Aug, 2010 18:19:05

Message: 1 of 8

Hi,

I need to communicate send/receive processed data from MATLAB to a small Flash program in real time.

I have only a minimal idea of how to set up communication between two processes. I googled, asked around etc. and right now I'm looking at TCP/IP as a means for solving the above problem.

ActionScript has a TCP/IP component it seems: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/XMLSocket.html

I have Instrument Control Toolbox installed. I had a look at this script: http://www.mathworks.com/matlabcentral/fileexchange/11802

So now I'm thinking it would be possible to setup communication with MATLAB and Flash in the application layer. Is this feasible? Or is there any other method? The important thing is that a large amount of data will be sent in real time.

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Walter Roberson

Date: 30 Aug, 2010 19:04:03

Message: 2 of 8

On 10-08-30 01:19 PM, Akhil Prem wrote:

> I need to communicate send/receive processed data from MATLAB to a small
> Flash program in real time.
>
> I have only a minimal idea of how to set up communication between two
> processes. I googled, asked around etc. and right now I'm looking at
> TCP/IP as a means for solving the above problem.
>
> ActionScript has a TCP/IP component it seems:
> http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/XMLSocket.html

You have a problem. According to the documentation, XMLSocket is for the
script to communicate with a TCP server. Matlab's Instrument Control Toolbox
is not able to act as a TCP server, only as a TCP client. With both ends only
able to be clients and not server, you cannot use TCP to communicate.

There is a user-contributed TCP/UDP toolbox for Matlab in the Matlab File
Exchange (FEX). I have not looked at the documenation for it; if it is not
able to act as a server either, then as the source is there you could in
theory modify it to act as a server; the underlying library that that
contribution uses has the ability, but I do not know how difficult it would be
to get Matlab to use it. (I suspect it would not be all that difficult for
someone who know TCP and knows C.)

The limitation I mention above in Matlab is on acting as a TCP server: it is
not a problem for Matlab to receive incoming UDP packets; I have not checked
to see if Flash is able to communicate via UDP.

UDP can sometimes be an advantage for "real-time work": it depends on the
relative importance of every packet getting through, vs the time-constraints.
UDP packets are more likely to be dropped on a busy network (because it is
assumed that the programs already take packet drops in to account), but UDP is
better for situations in which it is better to keep going with what you are
able to transfer quickly instead of waiting a while to get every packet
through in order.

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Akhil Prem

Date: 31 Aug, 2010 16:35:07

Message: 3 of 8

First of all,

Thank you Walter for that reply.

I assume this is the toolbox that you're talking about: http://www.mathworks.com/matlabcentral/fileexchange/345

It says in the description: "This toolbox can be used to set up TCP/IP connections or
send/receive UDP/IP packets in MATLAB. It can transmit data over the Intranet/Internet between MATLAB processes or other applications. It is possible to act as server and/or client and transmit textstrings, arrays of any datatype, files or MATLAB variables."

At least from the screenshot I think its able to act as as a server. If that be the case then the problem you mentioned will be resolved right?

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Walter Roberson

Date: 31 Aug, 2010 19:05:22

Message: 4 of 8

On 10-08-31 11:35 AM, Akhil Prem wrote:

> I assume this is the toolbox that you're talking about:
> http://www.mathworks.com/matlabcentral/fileexchange/345

That looks right.

> It says in the description: "This toolbox can be used to set up TCP/IP
> connections or send/receive UDP/IP packets in MATLAB. It can transmit
> data over the Intranet/Internet between MATLAB processes or other
> applications. It is possible to act as server and/or client and transmit
> textstrings, arrays of any datatype, files or MATLAB variables."
>
> At least from the screenshot I think its able to act as as a server.

It does seem to say that. I haven't tried it myself.

> If that be the case then the problem you mentioned will be resolved right?

Yes, provided that for your work flow it is okay if the Flash program
initiates the connections.


Your original posting mentioned "real time". There have been some postings
(including yesterday) that Matlab before R2010a had some difficulties in
buffering TCP/IP and was only able to reach 2 to 3 MB/s. It was not
immediately obvious whether that problem would apply to the FEX contribution
as well. A partial work-around stated yesterday was to turn the
line-terminator off even when using fread() .

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Patrick Mineault

Date: 31 Aug, 2010 20:01:20

Message: 5 of 8

"Akhil Prem" <ramu.premkumar@gmail.com> wrote in message <i5gsmp$3aq$1@fred.mathworks.com>...
> Hi,
>
> I need to communicate send/receive processed data from MATLAB to a small Flash program in real time.
>
> I have only a minimal idea of how to set up communication between two processes. I googled, asked around etc. and right now I'm looking at TCP/IP as a means for solving the above problem.
>
> ActionScript has a TCP/IP component it seems: http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/XMLSocket.html
>
> I have Instrument Control Toolbox installed. I had a look at this script: http://www.mathworks.com/matlabcentral/fileexchange/11802
>
> So now I'm thinking it would be possible to setup communication with MATLAB and Flash in the application layer. Is this feasible? Or is there any other method? The important thing is that a large amount of data will be sent in real time.

If you want Flash to call Matlab and receive the results an XMLSocket will do the trick. I remember a few years back we had to set up a terminal with a Flash GUI that would receive credit card swipes, and the card swipe software was written in Java. We ended up programming a Java XMLSocket server that the Flash client connected to. As Matlab has first class access to Java objects this should be feasible. Google XMLSocket Java. XMLSockets are kind of a pain to work with, though, IMHO.

If you're already running a webserver on the machine that's running the Flash client then one thing you could try is access Matlab through Flash via a gateway web page. For example, Python has a Matlab engine interface (google it). So you could write up a Python script that reads POST data and performs operations within the Matlab engine depending on the contents of this POST data and returns stuff for Flash to read. You could call up this Python script via URLLoader/URLRequest in Flash. Or if you want something more seamless you could communicate between Flash and Python via PyAMF, which translates Actionscript variables into their natural Pythonic equivalent.

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Akhil Prem

Date: 31 Aug, 2010 22:25:22

Message: 6 of 8

> Your original posting mentioned "real time". There have been some postings
> (including yesterday) that Matlab before R2010a had some difficulties in
> buffering TCP/IP and was only able to reach 2 to 3 MB/s. It was not
> immediately obvious whether that problem would apply to the FEX contribution
> as well. A partial work-around stated yesterday was to turn the
> line-terminator off even when using fread() .

2 to 3MB/s is more than adequate for me. I may have overstressed when I said "large quantities of data". And yes, Flash initiating the connections would do.

@ Patrick

The gateway solution sounds interesting although I'll have to pick up quite a few programming tidbits to actually realize something like that. But thank you for replying.

Also, since you mentioned Java I've been looking at this script:

http://www.mathworks.com/matlabcentral/fileexchange/25249

I have to convert MATLAB data to XML for this I think.

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Patrick Mineault

Date: 1 Sep, 2010 04:42:05

Message: 7 of 8

"Akhil Prem" <ramu.premkumar@gmail.com> wrote in message <i5jvgi$nvi$1@fred.mathworks.com>...
> > Your original posting mentioned "real time". There have been some postings
> > (including yesterday) that Matlab before R2010a had some difficulties in
> > buffering TCP/IP and was only able to reach 2 to 3 MB/s. It was not
> > immediately obvious whether that problem would apply to the FEX contribution
> > as well. A partial work-around stated yesterday was to turn the
> > line-terminator off even when using fread() .
>
> 2 to 3MB/s is more than adequate for me. I may have overstressed when I said "large quantities of data". And yes, Flash initiating the connections would do.
>
> @ Patrick
>
> The gateway solution sounds interesting although I'll have to pick up quite a few programming tidbits to actually realize something like that. But thank you for replying.
>
> Also, since you mentioned Java I've been looking at this script:
>
> http://www.mathworks.com/matlabcentral/fileexchange/25249
>
> I have to convert MATLAB data to XML for this I think.

That file will probably do the trick.

You could always use this toolbox or something similar to convert Matlab to XML:

http://www.mathworks.com/matlabcentral/fileexchange/4278-xml-toolbox

Flash has good support for XML since AS3, for example http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm .

Your other option is to dump the contents of your variable in some format both Matlab and Flash will understand inside of a CDATA block in XML, that is, don't send "real" xml through XMLSocket but just an XML document with a single node containing some parsable string. I would suggest looking into JSON:

http://github.com/mikechambers/as3corelib/
http://www.mathworks.com/matlabcentral/fileexchange/20565

Subject: Interfacing MATLAB and Flash with TCP/IP

From: Akhil Prem

Date: 1 Sep, 2010 17:29:21

Message: 8 of 8

One of my friends pointed me to a non XML Socket class for ActionScript:

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/net/Socket.html

 I think I'll try this out first. Will post my results once I'm done. Thanks again.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
actionscript Akhil Prem 30 Aug, 2010 14:24:06
tcpip Akhil Prem 30 Aug, 2010 14:24:06
flash Akhil Prem 30 Aug, 2010 14:24:06
rssFeed for this Thread

Contact us at files@mathworks.com