how to avoid tcpclient function open new ports all the time ?

3 views (last 30 days)
Hi,
does anyone know how to avoid Matlab open a new port, everytime something received via tcp ?
Cant find anything about it in the documentation but opening and closing a port decreases performance a lot.
t = tcpclient("192.168.11.1",1045,"Timeout",1,"ConnectTimeout",300);
%configureCallback(t,"byte",4,@readbytesfunction);
%t.BytesAvailabeFcn = @readbuffercallback; %if condition reached go to callback;
%readasync(t);;
%data2 = 0;
%data = int8(data2);
%write(t,data);
datareceive = read(t,4,"uint8");
app.ServerantwortEditField.Value = int2str(datareceive);
with matlab2021a the old functions doesnt work anymore.
I just want to open a socket connection with one port and pass data through continiously.
Thanks.

Answers (1)

Zinea
Zinea on 25 Feb 2024
When working with the ‘tcpclient’ object, it is not necessary for the client to open a new port each time it receives data. Instead, it should establish a single connection to the server and then continuously read from or write to that connection.
Here is an example code for your peruse:
while true
% Check if data is available to read
if t.BytesAvailable > 0
datareceive = read(t, t.BytesAvailable, "uint8");
app.ServerantwortEditField.Value = int2str(datareceive);
end
% Include a condition to break out of the loop and close the connection
end

Tags

Community Treasure Hunt

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

Start Hunting!