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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:27:00+00:00 2026-06-14T14:27:00+00:00

I would like to encode the contents of my byte[] variable before sending it

  • 0

I would like to encode the contents of my byte[] variable before sending it using asynchronous socket.

private void SendDatabaseObj(Socket handler, BuildHistoryClass buildHistoryQueryResult)
{
    byte[] byteData = ObjectToByteArray(buildHistoryQueryResult);

    // Begin sending the data to the remote device.
    handler.BeginSend(byteData, 0, byteData.Length, 0,
        new AsyncCallback(SendCallback), handler);
}

buildHistoryQueryResult is serialize using this function:

private byte[] ObjectToByteArray(BuildHistoryClass obj)
{
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    bf.Serialize(ms, obj);
    return ms.ToArray();
}

What would be “proper” encoding format because I am getting an exception in my receiver:

SerializationException was caught
The input stream is not a valid binary format. The starting contents (in bytes) are: 04-00-00-00-06-0F-00-00-00-04-54-72-75-65-06-10-00 …

Receiving side:

        private void ReceiveCallback_onQuery(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket 
                // from the asynchronous state object.
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;

                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.
                    state.sb.Append(state.buffer);

                    // Get the rest of the data.
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                        new AsyncCallback(ReceiveCallback_onQuery), state);
                }
                else
                {
                    // All the data has arrived; put it in response.
                    if (state.sb.Length > 1)
                    {
                        response_onQueryHistory = ByteArrayToObject(state.buffer);
                    }
                    // Signal that all bytes have been received.
                    receiveDoneQuery.Set();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

De-serializing function:

        private BuildHistoryClass ByteArrayToObject(byte[] arrayBytes)
        {
            MemoryStream ms = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            ms.Write(arrayBytes, 0, arrayBytes.Length);
            ms.Seek(0, SeekOrigin.Begin);
            BuildHistoryClass obj = (BuildHistoryClass)bf.Deserialize(ms);
            return obj;
        }
  • 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-14T14:27:02+00:00Added an answer on June 14, 2026 at 2:27 pm

    Your own code has a bug, and it may be the cause of the SerializationException.
    On the receiving side you have the following code and comment:

    // There might be more data, so store the data received so far.
    state.sb.Append(state.buffer);
    

    Yet later (after all data has been received) you have the following:

    // All the data has arrived; put it in response.
    if (state.sb.Length > 1)
    {
        response_onQueryHistory = ByteArrayToObject(state.buffer);
    }
    

    Note that you are de-serialising the state.buffer where you should be de-serialising whatever is in state.sb

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

Sidebar

Related Questions

I would like to encode some plain text using Ruby and the Crypt library
I would like to know how to html encode nodes in a asp.net treeview
I would like to use PKCS7 encryption to encode a value together with Java
I would like to point out before I get into this that I am
I would like to know how can we make the JSON based array using
I would like to send a file using the POST command available on almost
I am writing a server in C and I would like to encode file
Would like to parse IPv4 address from exit-addresses . Format of the file: ExitNode
Would like a for loop in jquery so that: For every hover_link: show hidden
Would like someone to take a look at my script and tell me where

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.