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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:37:07+00:00 2026-05-22T21:37:07+00:00

I’ve been learning C# by creating an app and i’ve hit a snag i’m

  • 0

I’ve been learning C# by creating an app and i’ve hit a snag i’m really struggling with.

Basicly i have the code below which is what im using to read from a network stream I have setup. It works but as its only reading 1 packet for each time the sslstream.Read() unblocks. It’s causes a big backlog of messages.

What im looking at trying to do is if the part of the stream read contains multiple packets read them all.

I’ve tried multiple times to work it out but i just ended up in a big mess of code.

If anyone could help out I’d appreciate it!

(the first 4bytes of each packet is the size of the packet.. packets range between 8 bytes and 28,000 bytes)

        SslStream _sslStream = (SslStream)_sslconnection;
        int bytes = -1;
        int nextread = 0;
        int byteslefttoread = -1;
        byte[] tmpMessage;
        byte[] buffer = new byte[3000000];
        do
        {
            bytes = _sslStream.Read(buffer, nextread, 8192);
           int packetSize = BitConverter.ToInt32(buffer, 0);
            nextread += bytes;
            byteslefttoread = packetSize - nextread;

            if (byteslefttoread <= 0)
            {
                int leftover = Math.Abs(byteslefttoread);
                do
                {
                    tmpMessage = new byte[packetSize];
                    Buffer.BlockCopy(buffer, 0, tmpMessage, 0, packetSize);

                    PacketHolder tmpPacketHolder = new PacketHolder(tmpMessage, "in");
                    lock (StaticMessageBuffers.MsglockerIn)
                    {
                        //puts message into the message queue.. not very oop... :S
                        MessageInQueue.Enqueue(tmpPacketHolder);
                    }
                }
                while (leftover > 0);

                Buffer.BlockCopy(buffer, packetSize , buffer, 0, leftover);
                byteslefttoread = 0;
                nextread = leftover;

            }

        } while (bytes != 0);
  • 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-22T21:37:07+00:00Added an answer on May 22, 2026 at 9:37 pm

    If you are using .Net 3.5 or later I would highly suggest you look into Windows Communication Foundation (wcf). It will simply anything you are trying to do over a network.

    On the other hand, if you are doing this purely for educational purposes.
    Take a look at this link. Your best bet is to read from the stream in somewhat smaller increments, and feed that data into another stream. Once you can identify the length of data you need for a message, you can cut the second stream off into a message. You can setup an outside loop where available bytes are being checked and wait until its value is > 0 to start the next message. Also should note, that any network code should be running on its own thread, so as to not block the UI thread.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

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.