How to allow TCP connections with While loop running

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.

 採用された回答

Kamden Kuykendall
Kamden Kuykendall 2023 年 9 月 8 日

0 投票

I ended up figuring this out through a fever dream.
Instead of using the while loop, I just have the end of the script run itself. That allows for the workspace to update, the client to connect, and client to swap to the next available server. I'll leave this up in the off chance someone else has the issue

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeThird-Party Cluster Configuration についてさらに検索

製品

リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by