Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:19:41+00:00 2026-06-14T16:19:41+00:00

How to correctly send a packet to a UDP server? I’m trying but it’s

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:19:43+00:00Added an answer on June 14, 2026 at 4:19 pm

    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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to send a raw UDP packet with Packet.Net but the device.SendPacket() function
I was trying to write a udp server who send an instance of a
For a project, I'm trying to send UDP packets from Linux kernel-space. I'm currently
I'm trying to send a UDP Multicast Packet to: 230.185.192.108 so everyone subscribed will
I am currently trying to send some data from and Android application to a
I want to send an object as a UDP packet and then receiving the
I'm trying to get my NAS server to wake from an S3 sleep state
I try to send FINS commands through UDP from PC (C#.NET) to PLC Ethernet
I'm currently working on a UDP socket application and I need to build in
I am trying to build a packet that will be sent via UDP. However

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.