Rules for Completing a Binary Read Operation
A read operation with fread blocks access to the MATLAB Command Window until
- The specified number of values is read. For UDP objects, DatagramTerminateMode must be off.
- The time specified by the Timeout property passes.
- A datagram is received (for UDP objects only when DatagramTerminateMode is on).
- The input buffer is filled.
- The EOI line is asserted (GPIB and VXI instruments only).
- The EOSCharCode is received (GPIB and VXI instruments only).
The "specified number of values" by default is the number that fill the input buffer.
You are not using UDP so datagram does not matter.
You are not using GPIB or VXI so EOl and EOSCharCode are not relevant.
So what is left is Timeout (what you are getting), or the input buffer being filled.
You have set the input buffer size to 5000.
What we are not given enough information to know is whether your output value h happens to be 5000 bytes. If it is less, then because you only write h (after the text line), the reader would sit waiting until timeout.
There are basically five ways of using TCP:
- don't send binary data (represent numbers by text) and send line terminators; or
- encode variable length binary data in forms that is guaranteed not to have whatever terminator you choose, and send that and the terminator to end the packet; or
- never send variable-length binary data: use a protocol that uses a fixed length of binary data at every point, and reads only that much data. Terminator should be off for this purpose; or
- only send variable length binary data as the last part of the conversation, and close down the socket to signal to the other end that there is no more data to be received. Terminator should be off for this purpose; or
- do not send binary data whose length is not known to the receiver. Instead, use a protocol that transmits the size first (possibly in binary) followed by the variable length data. The receiver reads the fixed length size, and uses that to adjust the information about how much binary data to read. Terminator should be off for this purpose.
Best Answer