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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:15:57+00:00 2026-05-16T15:15:57+00:00

I keep getting an SerializationException whenever I try to deserialize a list of doubles

  • 0

I keep getting an SerializationException whenever I try to deserialize a list of doubles from a NetworkStream:

End of Stream encountered before
parsing was completed

I have a simple client server architecture: my TcpTransportClient wraps the functionality of a TcpClient and I utilize two basic methods: Send (sends a message) and Receive (blocks until a message is received).

The Send function takes a Message, serializes it using a BinaryFormatter and sends the bytes via the NetworkStream.

public void Send(Message message)
{
    if (message == null)
    {
        throw new TransportException("Invalidate Parameter In Send Call");
    }

    if (_tcpClient == null || !_tcpClient.Connected)
    {
        throw new TransportException("Client Not Connected");
    }

    lock (_sync)
    {
        // Serialzie the message
        MemoryStream memStream = new MemoryStream();
        _serializer.Serialize(memStream, message);

        // Get the bytes of of the memory stream
        byte[] buffer = memStream.GetBuffer();

        // Write the message to the network stream
        NetworkStream networkStream = _tcpClient.GetStream();
        networkStream.Write(buffer, 0, buffer.Length);
        networkStream.Flush();
    }
}

The receive function reads bytes into a buffer from the NetworkStream, then deserializes the message by using a BinaryFormatter:

public Message Receive()
{
    if (_tcpClient == null || !_tcpClient.Connected)
    {
        throw new TransportException("Client Not Connected");
    }

    byte[] buffer;
    MemoryStream memStream = new MemoryStream();
    NetworkStream netStream = _tcpClient.GetStream();
    try
    {
        do
        {
            // Allocate a new buffer
            buffer = new byte[_tcpClient.ReceiveBufferSize];

            // Read the message buffer
            int sizeRead = netStream.Read(buffer, 0, buffer.Length);

            // Write the buffer to the memory stream
            memStream.Write(buffer, 0, sizeRead);

        } while (netStream.DataAvailable);

        // Reset the memory stream position
        memStream.Position = 0;

        // Deserialize the message
        object tmp = _deserializer.Deserialize(memStream); // <-- The exception is here!

        // Cast the object to a message
        return (Message)tmp;
    }
    catch (System.Exception e)
    {
        if (_tcpClient == null || !_tcpClient.Connected)
        {
            throw new TransportException("Client Not Connected");
        }
        else
        {
            throw e;
        }
    }
}

I have a basic sending thread:

TcpTransportClient client = new TcpTransportClient(GetLocalAddress(), servicePort);
client.Connect();

Thread sendThread = new Thread(() =>
{
    List<double> dataSet = new List<double>();
    for (double d = 0.0; d < 500.0; d++)
    {
        dataSet.Add(d);
    }

    while (true)
    {
        try
        {
            // Serialize the data set
            MemoryStream memStream = new MemoryStream();
            BinaryFormatter binFormat = new BinaryFormatter();
            binFormat.Serialize(memStream, (object)dataSet);

            // Create a message
            Message msg = new Message();

            // Hold the object bytes in an opaque field
            msg.SetFieldValue(1000, memStream.GetBuffer());

            // Set the message topic
            msg.SetTopic("client.dataSet");

            // Send the message
            client.Send(msg);

            // Sleep
            Thread.Sleep(3000);
        }
        catch (TransportException)
        {
            break;
        }
        catch(Exception)
        {
            //ignore it
        }
    }
});
sendThread.IsBackground = true;
sendThread.Start();

And a receive thread which gets started whenever a TcpClient is accepted:

public void HandleAcceptedClient(TcpTransportClient client)
{
    Thread receiveThread = new Thread(() =>
    {
        while (true)
        {
            try
            {
                Message msg = client.Receive();
                Trace.WriteLine("Server Received: " + msg.GetTopic());
                byte[] buffer = msg.GetFieldOpaqueValue(1000);
                MemoryStream memStream = new MemoryStream(buffer);
                BinaryFormatter binFormat = new BinaryFormatter();

                List<double> dataSet = (List<double>)binFormat.Deserialize(memStream);
                if (dataSet.Count == 500)
                {
                    Trace.WriteLine(DateTime.Now + ": OK");
                }
                else
                {
                    Trace.WriteLine(DateTime.Now + ": FAIL");
                }
            }
            catch (TransportException)
            {
                break;
            }
            catch(Exception)
            {
                // ignore it
            }

        }

    });
    receiveThread.IsBackground = true;
    receiveThread.Start();
}

The exception always occurs when I try to deserialize the message in Receive method of my TcpTransportClient, but the problem only occurs if I put some data in the data set. What’s the proper way to send a list of values over a network and successfully deserialize them on the receiving end?

P.S. I tried the solution in a nearly identical question, but it didn’t work: I’m still getting the same exception.

  • 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-16T15:15:58+00:00Added an answer on May 16, 2026 at 3:15 pm

    while (netStream.DataAvailable);

    That’s not correct. You should stop reading when the Read() call returns 0. The DataAvailable property just tells you whether or not the Read() call will block, waiting to allow the server to catch up.

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

Sidebar

Related Questions

I keep getting this error on emulator: I copied this from a tutorial and
I keep getting this error when I try to build my solution. I've tried
I keep getting... org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: You cannot use a spring-security-2.0.xsd schema with Spring
I keep getting this whenever I run the below code: Fatal error: Call to
I keep getting the same error when I try to set an attribute but
I keep getting this error from my code and I have no idea what
I keep getting an error when I try to export a release AIR build:
I keep getting a compilier error whenever I run this... I am quite sure
I keep getting this error when trying to delete a record from my table.
I keep getting this error when I try and run this script. It has

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.