jtcp(actionStr,vara​rgin)

Sends/receives TCP packets using Matlab's Java interface. Now handles matrices and cell arrays, etc.
16.5K Downloads
Updated 27 Mar 2017

View License

Transmission Control Protocol (TCP/IP) communications are used to send data from one computer to another over a network or from one application to another within a single computer.
The jtcp.m program uses Matlab's ability to call Java code to enable it to send and/or receive TCP packets. One Matlab session can communicate with another Matlab session (on the same machine or over the network) or it can communicate with a completely different program (again, on the same machine or over the network).
This latest version of jtcp.m uses Java's serialization functionality to write and read strings, matrices, cell arrays and primitives (that is, just about any Matlab variable type other than structures).

Serialization will cause errors if jtcp.m is talking to a program or piece of hardware that does not itself implement serialization. In this case, set the 'serialize' argument to false when requesting or accepting a connection. This will limit read and write operations to variables of type "int8", which most socket applications will be able to handle.

Update, 2013-04-19 (thanks to Qi Wang for pointing this out): java.lang.OutOfMemoryError occurs after a volume of data approximating the Java heap space size has been sent. This appears to be a bug in Java itself (see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6525563). Workaround is to call
jTcpObj.outputStream.reset;
every once in a while from whatever program you are calling the jtcp "WRITE" command from.

Cite As

Kevin Bartlett (2024). jtcp(actionStr,varargin) (https://www.mathworks.com/matlabcentral/fileexchange/24524-jtcp-actionstr-varargin), MATLAB Central File Exchange. Retrieved .

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

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.12.0.0

Modified to allow timeout of zero (i.e., an infinite timeout). This will cause Matlab to hang until the connection is made, so the user must also specify a value of true for the new optional parameter 'acceptZeroTimeouts'.

1.11.0.0

Added workaround for outOfMemoryError to program description.

1.10.0.0

Now issues meaningful error message when an attempt is made to send non-int8 data with serialization set to false.

1.9.0.0

Removed reference to older version of jtcp.m (no longer included in zip file) from description.

1.8.0.0

Incorporates code from the earlier, int8-only version of jtcp.m to make data serialization optional. This allows jtcp.m to communicate with programs/hardware that do not themselves implement serialization.

1.7.0.0

Re-wrote Java code to permit reading and writing of cell arrays, matrices, strings, etc. (previous version limited to type "int8").

1.6.0.0

Switched to sending/receiving messages of "int8" format, rather than strings. Enabled optional use of Rodney Thomson's DataReader java class for more efficient reading of incoming data.

1.5.0.0

Enabled call to error() if user inputs a value of mssg that is not a string.

1.4.0.0

Implemented suggestion from Derek Eggiman to replace while loop with built-in java socket timeout feature.

1.1.0.0

Call to serverSocket.close added in try/catch statement in order to fix bug, spotted by Jayson Brouchoud, that caused port to hang after an "accept" timed out.

1.0.0.0