Check tcpclient connection status
59 views (last 30 days)
Show older comments
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
t=tcpclient('172.1.1.102',50000,'Timeout',1,'ConnectTimeout',3);
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
try
write(t,0);
read(t);
disp('send succesfull');
catch ME
disp('connection lost');
disp(ME.identifier);
end
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!
0 Comments
Accepted Answer
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 .
5 Comments
Nitin Kumar
on 28 Sep 2024
Edited: Walter Roberson
on 28 Sep 2024
You can also use the system('ping ip_addr') if you are interested in the host being alive or not.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!