How to correctly send a packet to a UDP server? I’m trying but it’s not working an I did not received anything from the server.
What I need is to send a packet: int64|int|int
And receive a packet back from the server: int|int|int64
How to do it correctly? Thanks.
type
Tconnecting = record
a: int64;
b: integer;
c: integer;
end;
Treply_connect = record
a: integer;
b: integer;
c: int64;
end;
var
udp: TIdUDPClient;
send_data: TIdBytes;
received_data: TIdBytes;
i: integer;
packet: Tconnecting;
reply_packet: Treply_connect;
begin
packet.a := 41727101980;
packet.b := 0;
packet.c := RandomRange(1, 9999999);
SetLength(send_data, sizeof(packet));
Move(packet, send_data[0], sizeof(packet));
udp := TIdUDPClient.Create(nil);
try
udp.Host := 'server.com';
udp.Port := 1234;
udp.SendBuffer(send_data);
SetLength(received_data, 0);
i := udp.ReceiveBuffer(received_data, 5000);
Memo1.Lines.Add('Data reiceived! Len:'+IntToStr(Length(received_data))+',data:|'+BytesToString(received_data, 0)+'|');
finally
FreeAndNil(udp);
end;
You are not using
ReceiveBuffer()correctly. You are setting the size of the output buffer to 0 bytes, soReceiveBuffer()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 ofReceiveBuffer()will tell you how many bytes were actually read into the buffer.In other words, change this:
To this:
And change this:
To this:
On a separate note, Indy has a
RawToBytes()function you can use to copy your packet into aTIdBytes:And a
BytesToRaw()function for when you want to extract the received packet: