Check tcpclient connection status
이전 댓글 표시
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개)
카테고리
도움말 센터 및 File Exchange에서 TCP/IP Communication에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!