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

조회 수: 3 (최근 30일)
Kaan Inal
Kaan Inal 2021년 6월 14일
답변: Zinea 2024년 2월 25일
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.

답변 (1개)

Zinea
Zinea 2024년 2월 25일
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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by