MATLAB Answers

Check tcpclient connection status

80 views (last 30 days)
Hi, I'm currently using tcpclient command to establish tcpip communication with an external device (not tcpip() from instrument toolbox).
Sometimes, the connection is not stable and when I write data it returns error: 'An existing connection was forcibly closed by the remote host'.
Therefore, I want to implement a method to check the connection before I write / read from the connection. However, I just couldn't find much information regarding tcpclient.
I have attempted the following:
% connect
Here, before running the I deliberately disconnected Ethernet cable just want to test trigger the error:
% My intention: try a write / read to check if an error returns
disp('send succesfull');
catch ME
disp('connection lost');
For the first run 'An existing connection was forcibly closed by the remote host' still appears in the command window and the 'send successful' message is printed, catch statement is skipped. However, a second run will jump into catch statement though.
If I run the try-catch step by step, when it reaches 'write(t,0)' statement it returns the ''An existing ... remote host'', and continue to 'read(t)' statement, and jumps into catch statement.
I couldn't quite understand why this happened.
Thanks for your help very much!


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jan 2019
Edited: Walter Roberson on 18 Jan 2019
TCP was defined in a way that the connection is idle when there is no data to be transferred . TCP can survive literally disconnecting the wires for several days as long as there is no data being transferred during that time .
Because of this, if the last time you had a data exchange the connection worked, there is no way to ask "is the connection working right now?". Because the original TCP does not monitor connections that are not being used.
Therefore at the time you want to write the position there is no way to ask ahead of time "will this packet go through any time in the next several days ?" You can look at the connection open status property to see if it has already noticed a problem in the previous activity, but if it had then it more likely would have already notified you.
Most probable is that as far as it knows the connection is fine and tries to send the new data and during the sending process discovers a problem , generates a message, and sets the status to closed. If you then try to receive data on the connection you error because the connection is closed.
You can avoid the error as such by testing the open status after doing the write.
The particular message you are getting would occur in the situation where your end thinks the connection is fine and tries to send data and the other side replies back with "I already gave up on that connection !" That can occur if the other end tried to send data but discovered that it could not reach you for long enough that it gave up. That is, the situation can be triggered by an unstable connection where the other end failed taking to you and gave up on the connection .
However there is another notable circumstance: the situation can happen if the other end reboots or for any reason ends the program that was talking you.
For whatever reason there is no process listening to you now . The other end gave up on the connection or it aborted the program without a clean shutdown .


Show 1 older comment
Walter Roberson
Walter Roberson on 18 Jan 2019
Atch I just noticed you are using tcpclient rather than instrument control toolbox tcp. Instrument control has a number of properties not provided by tcpclient .
You might do better to look in the File Exchange for the tcpudpip contribution .
Yi Wan
Yi Wan on 18 Jan 2019
Do you mean the TCP/UDP/IP Toolbox 2.0.6 by Peter?

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!