MATLAB: Warning: Unsuccessful read: The specified amount of data was not returned within the Timeout period.

encryption and decryptioninstrumentation toolbox

Actually my project is Encryption of message at the sender site and Decryption at the receiver side. There is no problem at the sender side, but at the receiver side it is creating issues. The normal code for encryption and decryption is perfect, but the problem arises with the tcpip at the receiver. The decryption of the message at the sender side is not same when using the tcpip, but without using the tcpip the decryption is perfect. The message encrypted appears perfectly at the receiver side but problem arises afterwards as the decryption is not perfect and throwing error as unsuccessful read.
at sender:
t=tcpip('0.0.0.0',30000,'NetworkRole','Server');
t.OutputBufferSize=5000;
fopen(t);
fprintf(t,'%s\n',out3);
fwrite(t,h);
fwrite(t,st);
at receiver:
t=tcpip('LocalHost',30000,'NetworkRole','Client','TimeOut',3);
t.InputBufferSize=5000;
fopen(t);
nam=fscanf(t)
b1=fread(t);
please help me with this.

Best Answer

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:
  1. don't send binary data (represent numbers by text) and send line terminators; or
  2. 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
  3. 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
  4. 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
  5. 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.