How to allow TCP connections with While loop running
Show older comments
Hello, I am extremely new to network coding and know very little about MATLAB's TCP connection. Let me summarize my project for you. I am using MATLAB as a server that will be going onto a remote machine. I am connecting to this server through Unity and C#. I have the connection working, and data can be transferred to and from Unity and MATLAB if theyre connected already. My issue is when I try to begin automation as this will be a server that just runs in the background all the time.
mainServer = tcpserver(26000,"ConnectionChangedFcn",@connectionFcnMain,Timeout=20);
server(1) = tcpserver(26001,"ConnectionChangedFcn",@connectionFcn,Timeout=20);
server(2) = tcpserver(26002,"ConnectionChangedFcn",@connectionFcn,Timeout=20);
server(3) = tcpserver(26003,"ConnectionChangedFcn",@connectionFcn,Timeout=20);
while true
serversFull = [server(1).Connected server(2).Connected server(3).Connected];
firstEmpty = find(serversFull == 0,1);
if mainServer.Connected == 1
write(mainServer,server(firstEmpty).ServerPort,"uint8")
end
for i = 1:3
if server(i).NumBytesAvailable >0
input = read(server(i),server(i).numBytesAvailable);
%Take input, do stuff to get output
%this is where meat of the data processing goes so I removed for simplicity
write(server(i), output,"uint8");
end
end
end
This creates 4 servers, the main server and then 3 or more worker servers since matlab cant handle more than 1 client. It should find the first server open, and then send the port of that server back to the client so it can redirect to the worker server. My issue with this automation is that the client cant connect if the While loop is running, and wont be able to reconnect to the other server since the while loop is blocking that connection. What are my options here?
I've looked into asyncronous code but I don't know how to apply that here.
Accepted Answer
More Answers (0)
Categories
Find more on Third-Party Cluster Configuration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!