Variable Message lengths:
The TCP Receive and TCP Send blocks of the Simulink Real-Time / IP library have a "Length" port that contains the actual length of the received message. This allows to define the expected message to be longer than the actually received message by simply adding zeros to the actual message (eg: [message, 0 0 0]). Using the knowledge about the length of the message then allows to simply cut out the actual message without violating the buffer.
It seems like the Buffer is reseted in each iteration step not causing a problem for finding the message.
I was not able to use the TCP/IP blocks due to the variable message size.
Neither TCP nor TCP/IP blocks are included in code generation.
Stopping of the Simulation
The actual Simulink simulation runs in a much higher speed than real-time, so even setting the simulation time to inf does not fix the problem.
For this purpose both the Real-Time and the Desktop Real-Time environments have been introduced. Keep in mind, that the actual Real-Time environment requires an external machine that can be remote controlled through Simulink but has to have the code running inside (C-code generation must be valid).
On the other hand there is Desktop Real-Time environment. For more information about the three modes and the working principle, please check this: https://de.mathworks.com/matlabcentral/answers/536647-what-is-the-difference-between-normal-accelerator-and-external-mode-when-using-simulink-desktop-rea
I am still trying to establish the TCP/IP communication in Desktop Real-Time External. For further updates (if they exist) check here: https://de.mathworks.com/matlabcentral/answers/864025-simulink-for-real-time-control-of-a-robot-via-tcp-ip