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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:11:05+00:00 2026-06-06T05:11:05+00:00

First things first, let me explain my situation: I’m working on a client and

  • 0

First things first, let me explain my situation: I’m working on a client and a server in C# which use socket for communication.

For practical reason, I use the asynchronous part of both socket to transmit binary serialized objects from the client to the server and vice-versa.

My problem is that when I send too much object at once, the receiver object “stack” into the buffer and when I try to unserialize the buffer content, it give me only one object.

My question is : How can I separate each object from a buffer ?

Here is my ReceiveCallback function :

private void ReceiveMessageCallback(IAsyncResult asyncResult)
    {
        Socket socket = (Socket)asyncResult.AsyncState;
        try
        {
            int read = socket.EndReceive(asyncResult);
            if (read > 0)
            {
                Log("Reception of " + read + " Bytes");

                // Jumper is an object that I use to transport every message
                Jumper pod = Common.Serializer.DeSerialize<Jumper>(this.readbuf);

                Buffer.SetByte(this.readbuf, 0, 0);
                socket.BeginReceive(this.readbuf, 0, this.readbuf.Length, SocketFlags.None, new AsyncCallback(ReceiveMessageCallback), socket);

                //We fire an event to externalise the analyse process
                Receiver(pod, socket);
            }
        }
        catch (SocketException ex)
        {
            if (ex.SocketErrorCode == System.Net.Sockets.SocketError.ConnectionReset)
            {
                socket.Close();
                Log("Distant socket closed");
            }
            else
                Log(ex.Message);
        }
        catch (Exception ex)
        {
            Log(ex.Message);
        }
    }
  • 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-06T05:11:06+00:00Added an answer on June 6, 2026 at 5:11 am

    Deserialization will consume your buffer for one object, and will left ‘read pointer’ of the buffer at the start of the next object.

    My suggestion would be to either:

    • move data from socket stream to some other (memory) buffer and deserialize from there, and have control over whole ‘buffering’ by hand

    or

    • when you get a callback, call deserialize several times in a row, until you get an exception.

    Second approach will most likely fail because nobody can guarantee that you will always get WHOLE objects in one go, since stream that is attached to a socket is a sequence of bytes, and it can be broken at any point.

    Hope that makes some sense.

    EDIT:

    In fact, the proper thing to do this would be to encapsulate each ‘message’ (serialized object on sending) into a packet, in which for example you’ll have a small header telling you LENGTH of the packet, and upon reception, you will read the socket stream until you have complete packet data, then serialize. And then the next packet, and so on.

    EVEN MORE:

    Say you send data at a rate of 1000 bytes at the sender side into the socket. On the receiving side, you’ll probably NEVER get 1000 by 1000 bytes out of the socket, in fact you can expect that only under two conditions:

    • delay between each send is very large, and in between sends read occurs at receiver
    • pure accident
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright let me explain my situation first: I am part of an organization that
I'm working on my first 'real' F# assembly, and trying to do things right.
We have a very odd problem here with SVN. Let me first explain the
This might seem phrased oddly at first, but let me explain through example. Note
First things first: Server is an Apache running on Debian in a VMPlayer Host
One of the first things I learned in C++ was that #include <iostream> int
One of the first things I like to do when I make a site
One if the first things I learned when I started with C# was the
I know the bits just came out today, but one of the first things
I am reading some json output... just some integers. The first NSLog outputs things

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.