You are not using ReceiveBuffer()
correctly. You are setting the size of the output buffer to 0 bytes, so ReceiveBuffer()
will not have any memory to read the received data into. You need to pre-allocate the output buffer to the max size you are expecting. The return value of ReceiveBuffer()
will tell you how many bytes were actually read into the buffer.
In other words, change this:
SetLength(received_data, 0);
To this:
SetLength(received_data, SizeOf(Treply_connect));
And change this:
Memo1.Lines.Add('Data reiceived! Len:'+IntToStr(Length(received_data))+', data:|'+BytesToString(received_data, 0)+'|');
To this:
Memo1.Lines.Add('Data received! Len:'+IntToStr(i)+', data:|'+BytesToString(received_data, 0, i)+'|');
On a separate note, Indy has a RawToBytes()
function you can use to copy your packet into a TIdBytes
:
send_data := RawToBytes(packet, SizeOf(packet));
And a BytesToRaw()
function for when you want to extract the received packet:
BytesToRaw(received_data, reply_packet, SizeOf(reply_packet));