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

  • Home
  • SEARCH
  • 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 6471649
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T06:14:55+00:00 2026-05-25T06:14:55+00:00

I have this code: public static void SerializeRO(Stream stream, ReplicableObject ro) { MemoryStream serializedObjectStream

  • 0

I have this code:

public static void SerializeRO(Stream stream, ReplicableObject ro) {
    MemoryStream serializedObjectStream = new MemoryStream();
    Formatter.Serialize(serializedObjectStream, ro);

    MemoryStream writeStream = new MemoryStream();
    BinaryWriter bw = new BinaryWriter(writeStream);

    bw.Write(serializedObjectStream.Length);
    serializedObjectStream.Seek(0, SeekOrigin.Begin);
    serializedObjectStream.WriteTo(writeStream);
    serializedObjectStream.Close();

    writeStream.WriteTo(stream);

    bw.Close();
}

The line writeStream.WriteTo(stream); never finishes. The program gets to that line and won’t progress.

stream is always a NetworkStream. I’ve checked and I think it’s a valid object (at least it’s not null nor disposed).

So what’s going on?

  • 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-25T06:14:56+00:00Added an answer on May 25, 2026 at 6:14 am

    I tried your code – writing to a FileStream – and I always get zero bytes written to the stream. I don’t know why WriteTo would block on a NextWorkStream when there’s nothing to write, but that could be a problem

    When I extract the bytes from the MemoryStream and write them directly to stream everything works e.g. (I’m creating a binary formatter in the routine, it seems you already have a formatter).

    public static void SerializeRO(Stream stream, object ro)
    {
        MemoryStream serializedObjectStream = new MemoryStream();
        var f = new BinaryFormatter();
        f.Serialize(serializedObjectStream, ro);
    
        MemoryStream writeStream = new MemoryStream();
        BinaryWriter bw = new BinaryWriter(writeStream);
    
        var bytes = serializedObjectStream.ToArray();
        bw.Write(bytes.Length);
        bw.Write(bytes);
    
        var bwBytes = writeStream.ToArray();
    
        stream.Write(bwBytes, 0, bwBytes.Length);
    
        bw.Close();
    }
    

    However I’d do it like this with one MemoryStream and writing directly to stream, unless there’s something I don’t know about NetworkStream (this will of course close stream which may not be what you want)

    public static void SerializeRO(Stream stream, object ro)
    {
        byte[] allBytes;
        using (var serializedObjectStream = new MemoryStream())
        {
            var f = new BinaryFormatter();
            f.Serialize(serializedObjectStream, ro);
            allBytes = serializedObjectStream.ToArray();
        }
        using (var bw = new BinaryWriter(stream))
        {
            bw.Write(allBytes.Length);
            bw.Write(allBytes);
        }
    }
    

    Version that won’t close your NetworkStream (just don’t put the binary writer in a using statement or Close() it)

    public static void SerializeRO(Stream stream, object ro)
    {
        byte[] allBytes;
        using (var serializedObjectStream = new MemoryStream())
        {
            var f = new BinaryFormatter();
            f.Serialize(serializedObjectStream, ro);
            allBytes = serializedObjectStream.ToArray();
        }
        var bw = new BinaryWriter(stream)
        bw.Write(allBytes.Length);
        bw.Write(allBytes);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say, I have a code snippet like this: public static void main(String[] args) {
I have this code public static Boolean freq[] = new Boolean[Global.iParameter[2]]; freq[Global.iParameter[2]] = false;
I have this code: hubSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int
I have written this piece of code public class Test{ public static void main(String[]
I have a problem with this code public static void main(String[] args) throws IOException,
I have this section of code: public static void delete(File f) throws IOException {
I have this code in Global.asax.cs: public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute({resource}.axd/{*pathInfo}); routes.MapRoute(
I have this code: public static void Next() { Process[] processes = Process.GetProcessesByName(test); foreach
Okay, so i have this code i wrote: class Connection { public static StreamWriter
i have this code: public class Test{ arrayList<String> list = new ArrayList<String>(); String[][] temp_list;

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.