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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:42:53+00:00 2026-05-27T21:42:53+00:00

I am trying to convert an object to byte array & send it through

  • 0

I am trying to convert an object to byte array & send it through socket to other process
but process stops while deserialization of object it simply hangs.
what is wrong with code ?

client code

namespace client_2
{
    [Serializable]
    class data
    {
        public ArrayList list;
        public data()
        {
            list = new ArrayList();
        }

        public void add(object o)
        {
            list.Add(o);
        }

        public void disp()
        {
            foreach (object ob in list)
            {
                Console.WriteLine(ob.ToString());
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress adr = IPAddress.Parse("127.0.0.1");
            TcpClient cli = new TcpClient();
            cli.Connect(adr, 5577);
            NetworkStream strem = cli.GetStream();

            if (cli.Connected)
            {
                data d = new data();
                d.add("hello");
                d.add(44);

                Byte[] buffer = new Byte[1024];

                BinaryFormatter from = new BinaryFormatter();
                MemoryStream ms = new MemoryStream(buffer,true);

                from.Serialize(ms, d);
                strem.Write(buffer, 0, buffer.Length);
            }
            strem.Close();
            cli.Close();
        }
    }
}

server code

namespace server_2
{
    [Serializable]
    class data
    {
        public ArrayList list;
        public data()
        {
            list = new ArrayList();
        }

        public void add(object o)
        {
            list.Add(o);
        }

        public void disp()
        {
            foreach (object ob in list)
            {
                Console.WriteLine(ob.ToString());
            }
        }
    }
    class Program
    {
        public static void handleReq(object o)
        {
            TcpClient client = o as TcpClient;

            NetworkStream strm = client.GetStream();

            Byte[] buf = new Byte[1024];

            strm.Read(buf, 0, 1024);

            BinaryFormatter from = new BinaryFormatter();
            MemoryStream ms = new MemoryStream(buf, true);

            data d = (data)from.Deserialize(ms); // hangs here

            d.disp();

            strm.Close();
            client.Close();
        }
        static void Main(string[] args)
        {
            IPAddress addr = IPAddress.Parse("127.0.0.1");

            TcpListener listner = new TcpListener(addr,5577); ;
            listner.Start();

            while (true)
            {
                TcpClient client = listner.AcceptTcpClient();

                Task.Factory.StartNew(handleReq,client);
            }
             listner.Stop();
        }
    }
}
  • 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-27T21:42:54+00:00Added an answer on May 27, 2026 at 9:42 pm

    You need to read the whole networkstream not just 1024 bytes. You can put the networkstream directly in the Deserialize.

    It will fail anyhow because the class data on the serverside doesn’t match the class data on the client side (use a shared dll for client and server that holds the class data)

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

Sidebar

Related Questions

I'm trying to convert an Image object to a byte array then back to
I'm trying to convert the byteArray of a Sound Object to an array with
I am trying to convert class to byte but not worked. I receive a
I am trying to find a way, in C#, to convert a byte array
I am trying to convert a retrieved registry value from object to byte[] .
I am trying to automatically convert object's properties to DataTable (object is array and
I am using the JQuery json plugin and trying to convert a custom object
I am trying to convert a VISUAL USER OBJECT that was once embedded on
Trying to convert the following but am having difficulty. Can anyone see where I'm
I'm trying to convert some code that worked great in VB, but I can't

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.