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 1056465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:43:08+00:00 2026-05-16T17:43:08+00:00

i use a UDP-socket for sending/receiving objects. I serialize the objects into a byte

  • 0

i use a UDP-socket for sending/receiving objects.
I serialize the objects into a byte array send it and receive it with a client.
But before i can receive i have to allocate a byte array. I must do it before and then handle the Socket.Receive()-methode the array.
But my objects have a variable length. How can i discover the array-size?

Thank you

Edit: Here a example of my receive-methode:

BinaryFormatter bf = new BinaryFormatter();

public Object ReceiveUdpPacket()
        {
            byte[] objData = new byte[bufferSize];

            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 0);
            EndPoint ep = (EndPoint)ipep;
            MemoryStream ms = new MemoryStream();

            udpSocket.ReceiveFrom(objData, ref ep);

            ms.Write(objData, 0, objData.Length);
            ms.Seek(0, SeekOrigin.Begin);

            return (Object)bf.Deserialize(ms);
        }
  • 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-05-16T17:43:08+00:00Added an answer on May 16, 2026 at 5:43 pm

    The most typical method of doing this involves something along the lines of having the first few bytes of a data chunk be the chunk length.

    So, for example, if your object size is always under 65k or there abouts, you could send the array size through the socket first as a short (Look at using the BitConverter class). The receiver would read the first chunk it receives into a buffer (probably one roughly the size of a nominal UDP packet, somewhere around 512 bytes), grab the first two bytes, convert them to a short, and set up the buffer for the object. It would then continue to read from the socket until that buffer is full, and pass that buffer off to the de-serializer.

    This of course doesn’t cover error handing and such, but it’s the basic idea.

    EDIT:

    It’s been awhile since I did anything with UDP in .Net, but something like this might work (In somewhat psuedocode)

    var bigBuffer = new MemoryStream();
    
    //This should be run in a thread
    void UdpLoop()
    {
       while(!done)
       {
          var buffer = udpClient.Receive();
          bigBuffer.Write(buffer,buffer.Length);
       }
    }
    
    void MessageProcessor()
    {
       var messageLength = 0;
       while(!done)
       {
          if(bigBuffer.Length > 0 && messageLength == 0)
          {
             var lengthBytes = new byte[2];
             bigBuffer.Read(lengthBytes, 2);
             messageLength = BitConverter.ToInt16(lengthBytes); 
          }
          if(messageLength > 0 && bigBuffer.Length > messageLength)
          {
             var objectBuffer = new byte[messageLength];
             bigBuffer.Read(objectBuffer, messageLength);
             //Do deserialization here
          }
       }
    }
    

    That’s the basic process, ignoring locking due to multiple threads (which you will have to deal with since Receive bocks), and reading and writing from different spots in the memory stream. Hopefully this is enough to get you going in the right direction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sending data over an UDP socket and receive it in a loop with
I use UDP Sokckts in my client application. Here are some code snippets: SendIP
I have received data from UDP socket fd . How can I use this
I am trying to implement a simple UDP client and server. Server should receive
I'm currently using Socket class on both server and client to establish udp connection,
I'm writing a networking application that uses ASIO/UDP to send and receive between a
I am writing simple client-server program. Client send some messages to server using UDP
I discovered almost accidentally that my machine was sending and receiving UDP packets to
In my C++ application, I am using ::bind() for a UDP socket, but on
I'm trying to make some communication for my PIC devices and I use UDP

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.