MATLAB: Not able to connect to one of the Simulink Real-Time(SLRT) targets through standalone app

I have an app to connect to targets (Target 1 – 192.168.5.28 and Target 2 – 192.168.5.29) through Simulink Real-Time (SLRT). When running the app through app designer, I am able to connect to both targets individually i.e. editing the code for IP address of target 1 and run the app, connection works; then, editing the code for IP address of target 2 and run the app, connection works.
Now when I deploy the app as a standalone (with Target 1 IP address), connection works. When I deploy the app as a standalone (with Target 2 IP address), connection does NOT work. The app fails with error:
Could not open a connection to 192.168.5.28, port "21"
Error in => YPG_GUI_V01.mlapp at line 448
Note: The IP address is NOT of target 2. The standalone app fails with target 2 but IP address is of target 1. Port 21 can be due to 'openFTP' API in the code but not sure.
SLRT code:
app.tg = SimulinkRealTime.getTargetSettings(app.targetName);
if isempty(app.tg)
app.tg = SimulinkRealTime.addTarget(app.targetName);
end
%setAsDefaultTarget(app.tg);
app.tg.TcpIpTargetAddress='192.168.5.28';
app.tg.TcpIpTargetPort='22222';
app.tg.TcpIpSubNetMask='255.255.0.0';
app.tg.TcpIpGateway='255.255.255.255';
app.tg.TcpIpTargetDriver='Auto';
app.tg.TcpIpTargetBusType='PCI';
app.tg.TargetScope='Enabled';
app.tg.TargetBoot='DOSLoader'; %
app.tg.USBSupport='on';
%app.tg.EthernetIndex='3';
app.tg = slrt(app.targetName)

Best Answer

SimulinkRealTime.addTarget is not supported by MATLAB Runtime Compiler. So when generating a standalone app, the 'SimulinkRealTime.addTarget' line is skipped. This gives unexpected behavior. Try the following code which uses the default target and edit its configurations:
app.tg = SimulinkRealTime.getTargetSettings;
%setAsDefaultTarget(app.tg);
app.tg.TcpIpTargetAddress='192.168.5.28';
app.tg.TcpIpTargetPort='22222';
app.tg.TcpIpSubNetMask='255.255.0.0';
app.tg.TcpIpGateway='255.255.255.255';
app.tg.TcpIpTargetDriver='Auto';
app.tg.TcpIpTargetBusType='PCI';
app.tg.TargetScope='Enabled';
app.tg.TargetBoot='DOSLoader'; %
app.tg.USBSupport='on';
%app.tg.EthernetIndex='3';
app.tg = slrt