Efficient way to implement this UDP protocol for loopback IP
이전 댓글 표시
I'm working on a project where I need to send data via UDP protocol from one PC to another. Currently, I'm using two Matlab sessions for this purpose. See the following codes. The first session is
% Matlab Session 1 (Local Side)
clear all
clc
t=0:0.01:10;
u1 = udpport("LocalPort",8844);
pause(3)
for i = 1:numel(t)
v = 2;
w = .3;
input = num2str(v) + " " + num2str(w);
% send control input to remote side
write(u1,input,"string","HostName",8866);
flush(u1);
pause(.25)
end
The second session is
% Matlab Session 2 ( Remote Side )
u2 = udpport("LocalPort",8866,"LocalHost","HostName");
while true
if u2.NumBytesAvailable ~= 0
data = read(obj.u2,obj.u2.NumBytesAvailable,"string")
inputStr = split(data," ");
input = str2double(inputStr);
v = input(1);
w = input(2);
flush(u2);
end
end
The codes work but sometimes, at the remote side, instead of receiving two strings, it receives more. Flushing out the buffer doesn't solve the problem. How to fix and ameliorate the codes?
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 External Language Interfaces에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!