Socket between two Matlab programs

3 views (last 30 days)
AlphaDecay
AlphaDecay on 11 Jun 2019
Edited: AlphaDecay on 11 Jun 2019
I built a pair of matlab scripts that are supposed to communicate with each other over socket. The idea is that eventually function 1 will be communicating with an external program over socket in the same manner, so I've built function 2 as a dummy function to test the dynamics that rely on that communication. However, I'm having trouble with the data transfer.
Function 1 (only socket-related code shown)
...
...
%set up send & receive network connection
t1 = tcpip('0.0.0.0', 50000, 'NetworkRole', 'server'); % Dummy -> Sim connection
t2 = tcpip('localhost', 50001, 'NetworkRole', 'server'); % Dummy <- Sim connection
fopen(t1)
fopen(t2)
...
...
fwrite(t2, 1) %corresponds to header on dummy side
fwrite(t2, SNRs, 'double') %corresponds to learningIn on dummy side
while ~(t1.BytesAvailable) %wait for dummy to respond with output
pause(1)
end
hp = fread(t1, 1, 'int8'); %corresponds to learningOut on dummy side
...
...
Function 2 (Dummy)
t1 = tcpip('localhost', 50000); % Dummy -> Sim connection
t2 = tcpip('localhost', 50001); % Dummy <- Sim connection
fopen(t1)
fopen(t2)
sceneOver = 0;
while ~(sceneOver)
while ~(t2.BytesAvailable) %wait for Sim to send header & data
pause(1)
end
header = fread(t2, 1);
if header == 1
learningIn = fread(t2, t2.BytesAvailable, 'double'); %corresponds to SNRs on Sim side
elseif header == 9
sceneOver = true;
continue;
end
learningOut = binornd(1, .5, 1);
fwrite(t1, learningOut) %corresponds to hp on Sim side
end
My issues is this - the way it's here written here now, the line that reads in the double in function 2 takes roughly ~10 seconds to run. It does read in the correct input though.
I've tried getting rid of the 'double' qualifier in both the fwrite and the fread function (example of those lines below) as I've seen in some examples, but for some reason it only transfers 1 byte each time and I get the unit8 converted version of SNRs - even though I've seen examples transfer double data without the 'double' qualifier.
Function 1
fwrite(t2, SNRs) %corresponds to learningIn on dummy side
Function 2
learningIn = fread(t2, t2.BytesAvailable); %corresponds to SNRs on Sim side
Essentially, I'm fairly sure I'm doing something wrong as taking 10 seconds to read in a single double doesn't seem like correct operation, and I'm looking for advice on how to implement this correctly. Thank you.

Answers (0)

Community Treasure Hunt

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

Start Hunting!