File Exchange

image thumbnail

jtcp(actionStr,vara​rgin)

version 1.12 (20.2 KB) by

Sends/receives TCP packets using Matlab's Java interface. Now handles matrices and cell arrays, etc.

4.95238
23 Ratings

16 Downloads

Updated

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.

Comments and Ratings (77)

Kevin Bartlett

Hi André,
The problem is that the underlying Java routines are largely black boxes for me--I don't know enough about them to answer your question about the "SetObservable" property of the mssg object. It sounds like you have a better understanding of them than I do.

I'm sorry I can't be of more help.

André Guerra

Hello, Kevin.
So, I will reformulate my problem. I need to communicate between Java Clients and Matla instances. The reason I'm using your library is because I can transfer objects...I use the matlabcontrol library to evaluate the function your library provide. I finally got it to work...it seems to be the firewall blocking the connection and nothing related to your library. So, infinite timeouts work perfectly (just needed the small code modification I pointed earlier). Now another issue come along...I need to build a property listener over the jTcpObj in the client part in order to telll the client when to read the message. Is it possible to SetObservable the mssg output of the jTcpObj? Kind regards, André

Kevin Bartlett

@André Guerra, sorry, I don't know a way around this problem. This is the reason I have tried to avoid infinite timeouts--they tend to cause issues like the one you're having.

André Guerra

Hello, Kevin.
Yes. I also modified the jtcp.m code to accept infinite timeouts. Basically, timeout parameter can be >=0 and replaced 1000 seconds standard to 0 as well...since the underneath (java) classes support it. However, I verified that the when clients request connections there is always a timeout... (not under your code). I used a while cycle in between Server "acceptances" and try/catch block in all instances. When I want to shutdown the Server side with ctrl-c it is impossible. If it is improperly closed (terminating Matlab runtime), next time I try to connect the client it fails. Is there a way to reset the socket prior to cycle?
Best regards, Andre

Kevin Bartlett

@André Guerra, I have modified jtcp.m to allow the use of zero (i.e., infinite) timeouts. This will hang your Matlab session until the connection is made, so I have also created a new parameter, 'acceptZeroTimeouts', that must be set to true if zero timeouts are to be accepted by the program.

As for rethrowing the error after a timeout, use a try/catch block, as in this example:

try, jTcpObj = jtcp('accept',21566,'timeout',1); catch me; disp(['Error message is ' me.message]); end

Is that what you meant?

André Guerra

Is it possible to have a no timeout session? or how can I rethrow the accept connection code after timeout without crashing the jmi? Best regards,Andre.

Grg

Grg (view profile)

Kevin Bartlett

@Varun Prakash--Please attach transcripts of the sessions from both the client machine AND the server machine so that I can see what you're trying to do. Are you following the examples shown by "help jtcp"? I suggest you start with the "Send/receive strings" example.

I am unable to run the program in MATLAB(2014) version, It says it requires more input arguments. Please help me with this

kscott2k

In about 2 minutes I was able to setup and use this function to send data from one MATLAB session to another. Kudos to you. Well done and very useful!

Kevin Bartlett

@Jeremy--It doesn't look like I am able to delete comments, but that's okay. Your difficulties can serve as a reminder to others that a good first step in debugging jtcp.m is to start two Matlab sessions on the same computer and use the loopback address (127.0.0.1) as the IP address. This ought to sidestep any issues with the local firewall.

Glad to hear you were able to get this working under R2014a.

Jeremy

Jeremy (view profile)

Kevin, Disregard my last, and delete if possible. Darn corporate IT dept had some interesting configuration I had to undo. Turns out Matlab was being blocked by Windows.

Jeremy

Jeremy (view profile)

Hi Kevin,

I've tried this in Matlab 8.3.0.532 (R2014a) and I can't seem to get a connection. Is there some reason why this might be? Did I miss some version notes that might make this obvious?

For reference, I ran your example code for client/server for transferring a matrix and this is the error I get on the client side:

JTCPOBJ = jtcp('REQUEST','192.168.0.11',21566,'TIMEOUT',2000)
Error using jtcp>jtcp_request_connection (line 282)
jtcp.m--Failed to make TCP connection.
Java error message follows:
Java exception occurred:
java.net.SocketTimeoutException: connect timed out

at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)

at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)

at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)

at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)

at java.net.AbstractPlainSocketImpl.connect(Unknown Source)

at java.net.PlainSocketImpl.connect(Unknown Source)

at java.net.SocksSocketImpl.connect(Unknown Source)

at java.net.Socket.connect(Unknown Source)

Error in jtcp (line 230)
jTcpObj = jtcp_request_connection(host,port,timeout,serialize);

Regardless of what length of timeout I use, or what port I use, this seems to happen. I'm fairly certain that no firewalls are being imposed on me (subnet setup for this specific experiment of mine), but if you're certain it should work in 2014a, then I'll check again.

Kevin Bartlett

Well done, Carsten! I'm glad you were able to get it to work. Hopefully your solution will also be useful for other users. Cheers, Kevin.

Tyrone

Tyrone (view profile)

I did it :)
Still the jTCP serialization is off and i use my own one with the use of typecast.
The solution is to send a constant length of the data stream for example 40 bytes.
Then i used the NUMBYTES parameter in jTCP read and deserialized again with typecast.
Now i'm not flexible with the data i send but it's no problem because the sender and receiver know about that.

Kevin Bartlett

@Carsten. Sorry, I don't think I'm going to be able to help you with this. It isn't a problem I can duplicate, so debugging isn't something I can do. If you do figure it out, please let me know.

Tyrone

Tyrone (view profile)

Unfortunately not. I turned the jTCP serialization off but i need some sort of serialization to send my data.
I wrote my own then with some undocumented MATLAB functions.
If i try it on the same computer my program works but again if i use two different PCs i get an error.
At least it is a new one, a MATLAB and not java error: "Bad version or endian-key"
Maybe you know something about it, seems like jTCP can't read the data properly?!

Thanks

Kevin Bartlett

@Carsten--The OptionalDataException appears to be associated exclusively with serializable objects. I don't know why this is happening, but you could presumably get around this problem by setting 'serialize' to false and converting the payload data to int8 (see last example in jtcp.m header). Would this be an option for you?

Tyrone

Tyrone (view profile)

Hi Kevin,
i have a problem using the jTPC read function.

I get an 'java.io.OptionalDataException' if i try to use the read function in a timer function that works in a GUI.

If the timers period is >0.3 seconds it works without problems but i want a faster update.
The code that should be updated under 0.3 seconds is:

try
while true
mssgs = jtcp('read',jTcpObj);
mssg = mssgs(1); %mssgs is a cell array

if(~isempty(mssgs))
set(handles.timestamp_txt,'string',char(mssg));
end
end
catch me
disp(me);
end

Hope you can help me, thank you

Kevin Bartlett

@Geoffry Akien--I'm not able to duplicate your 2nd error (the one with jtcp_version01), so I'm not sure what to say about that. Your 1st error is really mysterious: a failed jtcp_request_connection() on my machine results in a "connect timed out" error, but the message you get says "Read timed out". I don't know the inner workings of Java well enough to understand why it should be attempting a read operation when you are requesting a connection, so I'm afraid I'm not going to be able to offer you any help here.

I want to use port 502 (I can't change this), but your code formally does not allow the user of ports <1025. I have a device which works just fine using tcpip in the instrument toolbox, but I was hoping to get rid of the dependency by using your code.

Naively changing this limit and using 2012bx64 on W7E I get the following:

>> j = jtcp('REQUEST', '10.10.10.4', 502)
Error using jtcp>jtcp_request_connection (line 293)
Java exception occurred:
java.net.SocketTimeoutException: Read timed out

at java.net.SocketInputStream.socketRead0(Native Method)

at java.net.SocketInputStream.read(Unknown Source)

at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)

at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown
Source)

at
java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown
Source)

at java.io.ObjectInputStream.readStreamHeader(Unknown Source)

at java.io.ObjectInputStream.<init>(Unknown Source)

Error in jtcp (line 230)
jTcpObj = jtcp_request_connection(host,port,timeout,serialize);

...but using the same approach with jtcp_version01 seems to work initially:

>> j = jtcp_version01('REQUEST', '10.10.10.4', 502)

j =

socket: [1x1 java.net.Socket]
remoteHost: '10.10.10.4'
port: 502
inputStream: [1x1 java.net.SocketInputStream]
dataInputStream: [1x1 java.io.DataInputStream]
outputStream: [1x1 java.net.SocketOutputStream]
dataOutputStream: [0 java.io.DataOutputStream]

...but then sending something gives this error:

jtcp_version01('WRITE', j, int8(c))
Error using jtcp_version01>jtcp_write (line 384)
Java exception occurred:
java.net.SocketException: Software caused connection abort: socket
write error

at java.net.SocketOutputStream.socketWrite0(Native Method)

at java.net.SocketOutputStream.socketWrite(Unknown Source)

at java.net.SocketOutputStream.write(Unknown Source)

at java.io.DataOutputStream.write(Unknown Source)

Error in jtcp_version01 (line 258)
jtcp_write(jTcpObj,mssg);

...although closing the connection seems to work without complaint.

Ian

Ian (view profile)

Zack, Psychtoolbox already contains a TCP/IP UDP interface, which i use to control neurophysiology amplifiers (has a client / server mode and serialisation of variables etc). We also interface with an eyelink eye tracker, but that has its own network code. The problems you'll have to deal with is synchronisation, how will you ensure accurate timing between psychtoolbox and the eye tracker?

Zack

Zack (view profile)

Hi Kevin,

I am currently trying to link up a mac laptop that will be presenting a stimulus in MATLAB (psychtoolbox), to a PC that has eye tracking software (that can be controlled by MATLAB). Is there an easy way to achieve this using jtcp?

I greatly appreciate your help.

-zack

Sally

Sally (view profile)

Brilliant! Thanks Kevin!

Kevin Bartlett

@Sally: You might use a try/catch block (if you're unfamiliar with these, type "doc try" on the command line).

Sally

Sally (view profile)

Thanks Kevin, so I tried that....but every time jtcp times out, it throws an error:
Error using jtcp>jtcp_accept_connection (line 327)
Java exception occurred:
java.net.SocketTimeoutException: Accept timed out

at java.net.PlainSocketImpl.socketAccept(Native Method)

at java.net.PlainSocketImpl.accept(Unknown Source)

at java.net.ServerSocket.implAccept(Unknown Source)

at java.net.ServerSocket.accept(Unknown Source)

Error in jtcp (line 232)
jTcpObj = jtcp_accept_connection(port,timeout,serialize);

So, if i could figure out a way to get it to timeout quietly and just get on with the rest of the code, that would work...

Suggestions?

Kevin Bartlett

@Sally: It may be possible to do what you want to do, but I don't know how. The only workaround I can think of is to simulate a longer timeout by repeatedly calling jtcp with a short timeout in a loop. You could then interrupt the attempt the normal way you interrupt a loop, i.e., using Ctrl-C, deleting a control file, a GUI callback, etc.

Sally

Sally (view profile)

If you set the server to 'accept' a transmission with a long timeout, say 20 minutes, how do you abort the process? CTL+C doesn't seem to do anything. It just hangs until the process times out. I'd like to be able to abort sooner without having to shut down all of matlab and restarting.

Kevin Wang

Any body tried this on Matlab 2011B and 2013A? I tried both today and got the following error message:

Error using jtcp>jtcp_request_connection (line 293)
Java exception occurred:
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.read(Unknown Source)
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
Error in jtcp (line 230)
jTcpObj = jtcp_request_connection(host,port,timeout,serialize);

Kevin Bartlett

@Qi--As near as I can tell, this is a bug with Java itself. I've added a description of the bug and the workaround to the program description above.

Thanks again for pointing this out.

Kevin Bartlett

@Qi--Thanks for the comment and especially for the test code. I'll see if I can replicate the error.

Qi Wang

Hi Kevin,

Your great script is exactly what I was looking for. However after playing with it for sometime, I found a problem. I use it to transmit a lot of data between two matlab processes. After a certain among of data has been transmitted, there will be an error happened as follow:

Error using jtcp>jtcp_write (line 373)
Java exception occurred:
java.lang.OutOfMemoryError: Java heap space

Error in jtcp (line 234)
jtcp_write(jTcpObj,mssg);

How much data can be transmited before the error happen depends on the java heap size setting in matlab. It seems the data was never cleared in the matlab java heap memory.

here is the code I use for testing:
%%%%%%%%%%%%%%%%%%%%%%%%
jTCPObj = jtcp('ACCEPT', 3000,'TIMEOUT',30*60*1000);
cnt = 0;
while 1
cnt = cnt + 1;

data = [];
while isempty(data)
data = jtcp('READ', jTCPObj);
end
disp(['data packet: ' num2str(cnt) ' received']);
end

%%%%%%%%%%%%%%%%%%%%%%%%
jTCPObj = jtcp('REQUEST','localhost',3000,'TIMEOUT',2000);
cnt = 0;
while 1
cnt = cnt + 1;
disp(['data packet: ' num2str(cnt)]);
data = rand(1024*1024, 1);
jtcp('WRITE', jTCPObj, data);
end

I am not familiar with java. I don't know it is I made some mistake or there is a problem related to java in your functions?

Michal

Michal (view profile)

JTCPOBJ = jtcp('REQUEST','127.0.0.1',5050)
Java exception occurred:
java.io.StreamCorruptedException: invalid stream header: 003E02F6
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)

Error in jtcp>jtcp_request_connection (line 293)
inputStream = java.io.ObjectInputStream(socketInputStream);

Error in jtcp (line 230)
jTcpObj = jtcp_request_connection(host,port,timeout,serialize);

Wow, amazing script!

I was discouraged at first since my application didn't support serialization. But as soon as I grasped that it was on by default, everything came together.

Also, adding the DataReader class shaved off over 80 seconds from my script. Reading byte by byte apparently takes alot of time :)

However, adding dynamic java paths to matlab yields alot of output as all commands are then searched for in the dynamic paths. I would suggest changing

dynamicJavaClassPath = javaclasspath('-dynamic');

to

dynamicJavaClassPath = javaclasspath('-all');

in order to avoid adding dynamic paths in case a static one already exists, as it did for me (edited classpath.txt and restarted Matlab).

Matlab2010

excellent!

use this if you dont have tcpip.m in the ICT toolbox.

Dean

Dean (view profile)

Musharaf Ali

Excellent ,I works great and easy to use ,Bless you

Mr Smart

Kevin Bartlett

Hi Nor, are you connecting two Matlab sessions together or trying to connect Matlab with some other software? Are you on Windows? Mac? Linux? What error message do you get when you fail to make a connection?

Probably the easiest way to play with jtcp.m is to get two Matlab sessions running on one computer (assuming your licence permits it). Assign one session the role of "client" and the other the role of "server and follow the example code in jtcp.m's help, with "client" lines being executed in the client session and the "server" lines being executed in the server session. EXECUTE THE LINES IN THE ORDER SHOWN IN THE HELP EXAMPLES. You can substitute a higher value than 2000 milliseconds for the timeout, if that makes things easier. I have used port 21566 in the examples; this port will have to be open in your firewall, or the firewall turned off.
Kevin.

Nor Muzakkir

i cant connect at all. i've tried adding Rules in the firewall (even tried when turning off the firewall) but still i keep failing to make connection.

i really dont understand much on networking procedures, but if anyone could help point out the steps to make this works (especially establishing connection) it would be very helpful.

Thanks so much.

Hey Jesse, I looked up and down the file exchange for something called CompressLib. I tried searches for: convert, compress, structures, binary, tcp, bytes, variables, and the like. Is CompressLib still on the exchange?

Kevin, I was planning as much, but then I was going to ask if you knew if it was possible to... do exactly what Jesse has suggested. Thank you Jesse. I will check out CompressLib and see if the combination works for what I need.

Jesse Hopkins

Jesse Hopkins (view profile)

Patrick and Ian, take a look at CompressLib on the file exchange. Using this you can serialize almost any matlab structure to an array of bytes, and deserialize it back to a matlab structure on the other side. You can do classes as well, although that takes some work in the class definition itself.

Kevin Bartlett

Hi Patrick. No, no plans to accommodate structures. You can easily pass any particular structure type by writing encode/decode functions to loop over the fields, passing each one to jtcp.m and re-assembling the structure on the other side. To make jtcp.m send and receive arbitrary structures, however, would require recursive programming to handle multiply-nested structures of all possible data types, and I didn't want to go that route.

Kevin,

I just downloaded your code, but I haven't had a chance to run it yet. I noticed in the description, you specifically say that it does not work on structures. Do you have plans to update this to send and receive structures?

Ian

Ian (view profile)

I'd love to be able to use this to send / receive class objects too. The way PNET does this is serialize an object to file (save it), then send the bytes, saved to file on the remote end and loaded back in. This is not efficient, but until TMW allows us to serialize objects properly, this works well enough.

Alan

Alan (view profile)

Thanks. Very nice.

I've found the problem: I was submitting int8('++addr'), but I should have submitted [int8('++addr') 13 10] to imitate CR.

Hello,

I am trying to use this tool to control a GPIB device over Ethernet using a Prologix' GPIB-Ethernet controller. Somehow I fail so far.

E.g., this controller can accept the command '++addr' and return the address of the device it is controlling. With a terminal program (I am using PuTTY), I type in '++addr' and get '5' in return. I am trying the code:

JTCPOBJ = jtcp('REQUEST', '131.220.157.23', 1234)
jtcp('WRITE',JTCPOBJ,int8('++addr'))
mssg = jtcp('READ', JTCPOBJ)

in the hope to see the response, but get a 1-by-0 matrix in response. Can you see what I am doing wrong? The IP address and the port are exactly the same as with the terminal.

Hello Kevin,

I tried to run the code and start a request from client to server but I got this error;
"??? Java exception occurred:
java.lang.IllegalArgumentException: timeout < 0

at java.net.PlainSocketImpl.setOption(Unknown Source)

at java.net.ServerSocket.setSoTimeout(Unknown Source)

Error in ==> jtcp>jtcp_accept_connection at 345
serverSocket.setSoTimeout(timeout);

Error in ==> jtcp at 256
jTcpObj = jtcp_accept_connection(port,timeout);"

What does that mean?

Timothy

I'm looking at MATLAB (+ Add ons as requried) as part of my dissertaion to model SCADA vulnerabilities, when they use TCP/IP (UDP) etc. This has popped up in my searching. Anybody any ideas if this file is suitable?

Tanyer Alan

Tanyer Alan (view profile)

@Kevin: Thanks for your response and for the adaptation of your code!
Just for the records: I was also running Matlab on Linux and I did have problems sending binary zeros.
I didn't try it on other operating systems.
I didn't try other strange characters either, just binary zeros and plain ASCII letters and punctuation.

Kevin Bartlett

@Wei Ren: Thanks for the feedback. I don't think I misunderstood you. You were finding that when you sent 0xB4 (decimal 180), you were receiving -74; you found that Edwin's fix of using
mssg(i) = read( jTcpObj.dataInputStream );
instead of
mssg(i) = jTcpObj.dataInputStream.readByte;
solved this problem.

However my point is that I never experienced this same problem using readByte(), which is why I think there may be some platform-dependent issues here.

I think that switching from transmitting chars to transmitting int8 values, as I've done in the newest version, ought to avoid some of these issues.

Let me know how it works for you and if any of the same problems crop up again.

Wei Ren

Kevin: I just wanted to make sure that you didn't misunderstood what i said last. 0xB4 is 180 in decimal. I was getting -74 until I corrected what Edwin pointed out.

Kevin Bartlett

@Matthias and Rei Wen: I think we may be running into some platform-dependent stuff (I'm running Linux). I never had the problem in which 0xB4 becomes -74, and binary zeroes were coming through fine. I've rewritten jtcp.m to send messages in int8 format, rather than as strings. I'm hoping this will result in less variability in its behaviour. I also took Matthias' suggestion of using write() instead of writeBytes(). I'd appreciate you letting me know how this works for you.

In addition, I have incorporated Rodney Thomson's DataReader java class into the program (its use is optional). Again, let me know how this works for you.

Hi Kevin and others.

I had a similar problem when I wanted to send strings which contained binary zeros (\0) as separators.
This didn't work with writeBytes() because the binary zero was interpreted as end-of-string.
Also, when I analyzed the IP traffic with wireshark, I saw that sometimes (strangely not always) an IP packet was sent for every single character of my message-string, which seemed quite inefficient to me.

So I changed line 351 from

jTcpObj.dataOutputStream.writeBytes(char(mssg));

to

jTcpObj.dataOutputStream.write(uint8(mssg), 0, length(mssg));

... where the second argument is an offset (which I chose to be 0).

This worked perfectly for me, I think it could also work for others, but of course I'm not sure about any side-effects or pitfalls ...

BTW, I didn't didn't care about receiving data, because I used another application for that.

Thanks for the this great m-file, it was very useful!

Wei Ren

so using mssg(i) = read( jTcpObj.dataInputStream ) actually solved the problem of why 0xB4 was becoming -74. (Thanks Edwin)

So this is what i do sending hex data
mssg = sscanf(' 02 04 05 B4 01 01 04 02','%x');
jTcpObj.dataOutputStream.write(mssg);
jTcpObj.dataOutputStream.flush;

jTcpObj.dataOutputStream.writeBytes(mssg) works as well and both would work without using the flush class too.

on the receiving end
convert the mssg to hex using dec2hex(mssg)

Kevin Bartlett

@Wei Ren: I saw Rodney's new DataReader class. I might try to incorporate it into jtcp to make it more efficient, but I haven't gotten around to it yet.

Regarding sending/receiving hex data: sorry, no clue. I'll keep playing with jtcp to see if it can be expanded to data types other than strings. If you figure anything out in the meantime, please let me know.

Wei Ren

I forgot to note that even though it's sent correctly through wireshark, im still unable to read it correctly yet on matlab.
any ideas on how 0xB4 becomes -74?

Wei Ren

so according to wireshark
mssg = sscanf(' 02 04 05 B4 01 01 04 02','%x');
using either
jTcpObj.dataOutputStream.writeBytes(char(mssg));
or
jTcpObj.dataOutputStream.write(mssg);
show as 02 04 05 B4 01 01 04 02 sent correctly. which is what I want.

also have you tried
data_reader = DataReader(d_input_stream);
message = data_reader.readBuffer(bytes_available);
Rodney had that new in his 2nd update of his syntax.
I tried adding that to your script but couldnt get it to work right.

Wei Ren

The thing is that I need my server to communicate to an application that will need it in HEX. so :(

Kevin Bartlett

Hexadecimal should be easy, since (as I understand it) Matlab stores hexadecimal numbers as strings anyway. If you have a decimal number to send, you can convert it to a string with sprintf or num2str. You can also switch back and forth from hex to decimal with hex2dec and dec2hex.

Wei Ren

Hey thanks for the reply.
I'll try to see what matlab can do with changing the formats correctly how I need it. This is going to take some trial and error.

I wish it was more well documented.

Kevin Bartlett

Edwin, changing line 396 to
"mssg(i) = read( jTcpObj.dataInputStream );"
might well be the right thing to do, but I can't say for sure that this won't cause more problems.

When I wrote jtcp.m, I limited it to handling strings because I don't really understand the internal workings of Sockets very well. Making it handle binary, hex, etc., seemed overly ambitious. I figured that for most applications the user could convert data to strings on one end and back to the original format (e.g., binary or hex) on the other.

I originally had a line testing to see if the user had entered a value of mssg that was not a string, but for some reason I commented it out. I have re-enabled this line, so now a non-string message will raise an error. Anybody who really wants to use jtcp.m to transmit binary or hex will have to re-comment out this test.

If there are any Sockets experts out there who want to take on the challenge of enhancing jtcp.m to handle other data types, I would encourage them to copy my code, build an improved version and post it to the File Exchange themselves, since this sort of thing is well outside my area of expertise.

Wei Ren, I hope this answers your question as well.

Edwin Carter

Great little example.
The only problem I've found is that binary numbers >127 get mapped to zero on read. I found that replacing
mssg(i) = jTcpObj.dataInputStream.readByte;
with
mssg(i) = read( jTcpObj.dataInputStream );
(in jtcp_read(), line 396)
solves this issue, but I don't know if this is the right thing to do...

Wei Ren

Can the output be anything else besides char? like hexidecimal for example?

Derek Eggiman

Very nice example. However I've noticed your read function could be improved or perhaps cleaned up a little. Instead of the while loop for timeout you could do the following.

import java.net.Socket
import java.net.InetSocketAddress

%Set SocketAddress
sAddress = InetSocketAddress(Host,Port);

%Establish unconnected socket.
socket = Socket();

%connect
try
socket.connect(sAddress,timeout);
end

Kevin Bartlett

Hi Jayson,

No, it wasn't your fault; I failed to close the server socket in the event of a timeout. I've posted an updated version that I hope fixes the problem, but it hasn't appeared online yet. Look for an "Updates" section to appear in the next day or so below this "Comments and Ratings" section and then download the new version.

Glad you're finding it useful! And thanks for your help!

Kevin.

Kevin,

This tool has been extremely useful for me. There is one problem I haven't gotten around yet. I'm regularily checking to see if other PCs have "logged on" so I'm doing a jtcp('accept') command triggered off a timer. The problem is that if the PC is not logging on, then I get a timeout as expected, but it hangs the port. I added a try/catch, but this kicks the jtcp routine back before it closes the port so I have the same problem. I'm sure it's my lack of how to use the tool properly, but if you have any suggestions, I would appreciate them.

Thanks,
Jayson

Updates

1.12

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

Added workaround for outOfMemoryError to program description.

1.10

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

1.9

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

1.8

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

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

1.6

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

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

1.4

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

1.1

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.

MATLAB Release
MATLAB 7.8 (R2009a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Win prizes and improve your MATLAB skills

Play today