how to disconnect tcp/ip connection?

I made tcp/ip server and client like this
server = tcpserver('0.0.0.0',111);
and another app
cli = tcpclient('localhost',111);
It is perfectly connected.
So, how to disconnect TCP/IP form client?
and how to disconnect from server?

 Accepted Answer

Hi,
You can close the TCP/IP connection by clearing the 'tcpclient' object using the 'clear' function .
Please refer to the following documentations to get a better idea on this :
Hope this helps!

5 Comments

Jae-Hee Park
Jae-Hee Park on 15 Dec 2021
Edited: Jae-Hee Park on 15 Dec 2021
I tried but does not work.TT
It still connected.
I used “clear” to disconnect the TCP/IP link in .m code, and it seems work well. But in the MATLAB APP designer, "clear" command could not work.
clc;clear;close all;
t_client = tcpclient('192.168.137.100',5551,"Timeout",5);
data_send=input('Please input the word you want to send. \n','s');
% only supported by 2021b or later
write(t_client,data_send,"string");
% t_client.NumBytesAvailable could not be updated immediately
pause(0.1)
data_recv = read(t_client,t_client.NumBytesAvailable,"string")
clear t_client
% Button pushed function: StartButton
function StartButtonPushed(app, event)
app.t_client = tcpclient('192.168.137.100',5551,"Timeout",5);
end
% Button pushed function: PauseButton
function PauseButtonPushed(app, event)
data_send='hello';
write(app.t_client,data_send,"string");
pause(0.1);
data_recv = read(app.t_client,app.t_client.NumBytesAvailable,"string");
fprintf('%s\n',data_recv);
end
% Button pushed function: StopButton
function StopButtonPushed(app, event)
clear app.t_client;
end
Use
delete(app.t_client);
instead in MATLAB APP designer.
https://ww2.mathworks.cn/matlabcentral/answers/449835-app-designer-how-do-i-clear-all-variables
@chuck shen Thank you.
delete(app.client);
Above command works clear the values of app.client but do not erase the app.client.
%ex)
delete(app.client);
isa(app.client,'tcpclient'); % is true
There is no way to completely erase the app.client?
I was dealing with the same issue, using appDesigner and create a handle (property of my app) to a tcpclient object. The new solution was the "old solution" (used in "tcpip" objects).
delete(app.client)
clear app.client
The validation isa(app.client,'tcpclient') as true is not a problem at all. You can create other tcpclient object and handling this object in "app.client".

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!