Exiting form readline() in TCPIP server before timeout occurs
5 views (last 30 days)
Show older comments
I am using the App Designer to develop a TCPIP server (OS: Windows 11). I have to receive command strings from a remote Cient but I must be free at any moment to stop the server and exit from the app by using a specific button: STOPbutton. This is my code:
%Code that executes after component creation
function startupFcn(app)
app.myServer = tcpserver("::",4000,"Timeout",1000);
configureTerminator(app.myServer,"CR")
while(1) % Infinite loop
try
command = readline(app.myServer); % A command string is received
% The command is parsed
%
%
end
end
% Button pushed function: StopButton
function STOPButtonPushed(app, event)
clear app.myServer
app.delete
end
Debugging the script in the App Designer pushing the STOPbutton (before the 1000 secs timeout expires) the result is not the expected one. The UIfigure user interface closes but the debugging session doesn't stop and the only way to restart the session is by using a ctrl-C in the Matlab command window.
This is the message i am getting in the command window:
Operation terminated by user during matlabshared.asyncio.internal.Stream/wait
In matlabshared.transportlib.internal.asyncIOTransportChannel.AsyncIOTransportChannel/waitForDataAvailable
In matlabshared.transportlib.internal.asyncIOTransportChannel.AsyncIOTransportChannel/readUntil
In matlabshared.transportlib.internal.GenericTransport/readUntil
In matlabshared.transportclients.internal.StringClient.StringClient/readRaw
In matlabshared.transportclients.internal.StringClient.StringClient/read
In matlabshared.transportlib.internal.client.ClientImpl/readline (line 139)
data = read(obj.StringClient, precision);
In matlabshared.transportlib.internal.client.GenericClient/readline (line 631)
data = readline(obj.ClientImpl, varargin{:});
In tcpserver.internal.TCPServer/readline (line 595)
data = readline(obj.Client,varargin{:});
In Mat_server_app/startupFcn (line 37)
command = readline(app.myServer);
In matlab.apps.AppBase/runStartupFcn (line 69)
ams.runStartupFcn(app, startfcn);
In Mat_server_app (line 149)
runStartupFcn(app, @startupFcn)
It seems that by deleting the tcpserver object the readline function is not terminated and some process remains active.
By compiling a Standalone Destktop App and running the App the anomaly has been confirmed. By pushing the STOPbutton the UIfigure user interface closes but a the process related to the App is always running (as reported by the by the Windows 11 task manager ). The only way to restart the server is to kill such process in the task manager.
Any idea about how to force the readline process to end before its timeout expires ?
0 Comments
Answers (1)
chrisw23
on 20 Feb 2023
Edited: chrisw23
on 20 Feb 2023
try to implement the BytesAvailableFcn callback before starting the readline function
flush the buffer and delete ther server when the app will be closed
2 Comments
chrisw23
on 21 Feb 2023
You call readline() without knowing if there's any data to be read and this probably results in wait/timeout state. This may be the effect you see when trying to close your app.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!