Detection of disconnected server when using tcpclient() and write()
I am using the tcpclient() function to establish a connection to a server I built and I would like to be able to detect when the server disconnects. My current understanding is that the way to do that is to attempt a write and if the write fails, the connection can be considered as disconnected.
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434); %connects to server
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
The above code works fine as expected.
Next, I run the same code, but I disconnect the server after the connection is established but before the write is sent.
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434);
% I disconnect the server before attempting the first write
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to disconnected server. No error raised.
write(t,msg); % sends message to disconnected server. "Error using asyncio.MessageHandler/onError" is shown in command window
write(t,msg); % sends message to disconnected server. "An established connection was terminated. Please create a new TCPCLIENT." is shown in command window
write(t,msg); % sends message to disconnected server. "Invalid or deleted object." is shown in command window.
I can handle the last 2 errors using a try/catch, but I am not able to handle the "Error using asyncio.MessageHandler/onError" error with a try/catch.
I think this is because the error is raised asynchronously. I would like to be able to catch this error to avoid error messages in the final application.
According to the tcpclient page, there is a callback property called ErrorOccurredFcn that seems to be built to handle that.
However, even with the following code, I still get the same "Error using asyncio.MessageHandler/onError" error and my error handler function is not executed.
function error_handler()
disp("Asynchronous error handled"); % never executed
end
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434);
t.ErrorOccurredFcn = @error_handler;
% I disconnect the server before attempting the first write
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to disconnected server. No error raised.
write(t,msg); % sends message to disconnected server. "Error using asyncio.MessageHandler/onError" is shown in command window
So, I still get an error in the command window after using the ErrorOccurredFcn callback property.
The read() command does not seem to raise an exception whenever the server is disconnected.
So, what is the proper way to detect if the server is disconnected without getting an error in the command window?I am using the tcpclient() function to establish a connection to a server I built and I would like to be able to detect when the server disconnects. My current understanding is that the way to do that is to attempt a write and if the write fails, the connection can be considered as disconnected.
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434); %connects to server
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
The above code works fine as expected.
Next, I run the same code, but I disconnect the server after the connection is established but before the write is sent.
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434);
% I disconnect the server before attempting the first write
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to disconnected server. No error raised.
write(t,msg); % sends message to disconnected server. "Error using asyncio.MessageHandler/onError" is shown in command window
write(t,msg); % sends message to disconnected server. "An established connection was terminated. Please create a new TCPCLIENT." is shown in command window
write(t,msg); % sends message to disconnected server. "Invalid or deleted object." is shown in command window.
I can handle the last 2 errors using a try/catch, but I am not able to handle the "Error using asyncio.MessageHandler/onError" error with a try/catch.
I think this is because the error is raised asynchronously. I would like to be able to catch this error to avoid error messages in the final application.
According to the tcpclient page, there is a callback property called ErrorOccurredFcn that seems to be built to handle that.
However, even with the following code, I still get the same "Error using asyncio.MessageHandler/onError" error and my error handler function is not executed.
function error_handler()
disp("Asynchronous error handled"); % never executed
end
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434);
t.ErrorOccurredFcn = @error_handler;
% I disconnect the server before attempting the first write
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to disconnected server. No error raised.
write(t,msg); % sends message to disconnected server. "Error using asyncio.MessageHandler/onError" is shown in command window
So, I still get an error in the command window after using the ErrorOccurredFcn callback property.
The read() command does not seem to raise an exception whenever the server is disconnected.
So, what is the proper way to detect if the server is disconnected without getting an error in the command window? I am using the tcpclient() function to establish a connection to a server I built and I would like to be able to detect when the server disconnects. My current understanding is that the way to do that is to attempt a write and if the write fails, the connection can be considered as disconnected.
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434); %connects to server
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
write(t,msg); % sends message to server. Server receives the data.
The above code works fine as expected.
Next, I run the same code, but I disconnect the server after the connection is established but before the write is sent.
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434);
% I disconnect the server before attempting the first write
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to disconnected server. No error raised.
write(t,msg); % sends message to disconnected server. "Error using asyncio.MessageHandler/onError" is shown in command window
write(t,msg); % sends message to disconnected server. "An established connection was terminated. Please create a new TCPCLIENT." is shown in command window
write(t,msg); % sends message to disconnected server. "Invalid or deleted object." is shown in command window.
I can handle the last 2 errors using a try/catch, but I am not able to handle the "Error using asyncio.MessageHandler/onError" error with a try/catch.
I think this is because the error is raised asynchronously. I would like to be able to catch this error to avoid error messages in the final application.
According to the tcpclient page, there is a callback property called ErrorOccurredFcn that seems to be built to handle that.
However, even with the following code, I still get the same "Error using asyncio.MessageHandler/onError" error and my error handler function is not executed.
function error_handler()
disp("Asynchronous error handled"); % never executed
end
% I start the server before the first matlab command
t= tcpclient("192.168.10.188", 65434);
t.ErrorOccurredFcn = @error_handler;
% I disconnect the server before attempting the first write
msg = uint8([1,2,3]); % basic message with 3 bytes
write(t,msg); % sends message to disconnected server. No error raised.
write(t,msg); % sends message to disconnected server. "Error using asyncio.MessageHandler/onError" is shown in command window
So, I still get an error in the command window after using the ErrorOccurredFcn callback property.
The read() command does not seem to raise an exception whenever the server is disconnected.
So, what is the proper way to detect if the server is disconnected without getting an error in the command window? tcpclient, erroroccurredfcn, write, disconnect, disconnection MATLAB Answers — New Questions