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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:59:36+00:00 2026-05-16T14:59:36+00:00

var buffer = new byte[short.MaxValue]; var splitString = new string[] {\r\n}; while (_tcpClient.Connected) {

  • 0
var buffer = new byte[short.MaxValue];
var splitString = new string[] {"\r\n"};
while (_tcpClient.Connected)
{  
  if (!_networkStream.CanRead || !_networkStream.DataAvailable)
    continue;

  var bytesRead = _networkStream.Read(buffer, 0, buffer.Length);
  var stringBuffer = Encoding.ASCII.GetString(buffer, 0, bytesRead);
  var messages = 
     stringBuffer.Split(splitString, StringSplitOptions.RemoveEmptyEntries);
  foreach (var message in messages)
  {
    if (MessageReceived != null)
    {
      MessageReceived(this, new SimpleTextClientEventArgs(message));
    }
  }
}

Problem is that even with a buffer as big as short.MaxValue, you can actually fill the buffer. When you split the string that you create from the buffer, the last string gets chomped, and the rest of it comes with the next read.

I was thinking of creating a buffer large enough for a single line (which according to RFC2812 is 512 chars), extracting a substring up until the first “\r\n”, then array-copying the rest of the data to the beginning of the buffer and using the offset parameter to read more data onto the end of the data that wasn’t extracted last iteration. Sorry if that was hard to follow…

Is that the best solution, or am I missing the obvious here?

  • 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-16T14:59:36+00:00Added an answer on May 16, 2026 at 2:59 pm

    So here’s how I ended up solving it:

    var buffer = new byte[Resources.MaxBufferSize];
    var contentLength = 0;
    while (_tcpClient.Connected)
    {
      if (!_networkStream.CanRead || !_networkStream.DataAvailable)
        continue;
    
      var bytesRead = _networkStream.Read(buffer, contentLength, buffer.Length - contentLength - 1);
      contentLength += bytesRead;
      var message = string.Empty;
      do
      {
        message = ExtractMessage(ref buffer, ref contentLength);
        if (!String.IsNullOrEmpty(message))
        {
          if (MessageReceived != null)
          {
            MessageReceived(this, new SimpleTextClientEventArgs(message));
          }                        
        }
      } while (message != string.Empty);
    }
    
    private string ExtractMessage(ref byte[] buffer, ref int length)
    {
      var message = string.Empty;
      var stringBuffer = Encoding.UTF8.GetString(buffer, 0, length);
      var lineBreakPosition = stringBuffer.IndexOf(Resources.LineBreak);
      if (lineBreakPosition > -1)
      {
        message = stringBuffer.Substring(0, lineBreakPosition);
        var tempBuffer = new byte[Resources.MaxBufferSize];
        length = length - message.Length - Resources.LineBreak.Length;
        if (length > 0)
        {
          Array.Copy(buffer, lineBreakPosition + Resources.LineBreak.Length, tempBuffer, 0, length);
          buffer = tempBuffer;
        }
      }
      return message;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: public byte[] SerializeToBlob() { using (var buffer = new MemoryStream())
private static Int64 NextInt64(Random rnd) { var buffer = new byte[sizeof(Int64)]; rnd.NextBytes(buffer); return BitConverter.ToInt64(buffer,
var guidComPorts = Guid.Empty; UInt32 dwSize; IntPtr hDeviceInfo; var buffer = new byte[512]; var
I read the input buffer from a console application (CMD) like this: var pBuffer
var ref1 = new Firebase(http://gamma.firebase.com/myuser/123,456); ref1.set(123,456); var on1 = ref1.on(value, function(snapshot) { console.log(snapshot.val()); });
I got this (i also tried crStream.CopyTo(ms)): var cryptic = new DESCryptoServiceProvider(); cryptic.Key =
var gethtml = $(#topmenu > li.l89 a).text().split(' ')[1].replaceWith('Any World'); The text is not changing
var Foo = Foo || {}; Foo.Controller = (function ($) { var $page =
var SalesforceOAuthPlugin = { /** * Obtain authentication credentials, calling 'authenticate' only if necessary.
var fruit = [apple,pear,pear,pear,banana]; How do I remove all pear fruit from this array?

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.