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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:09:22+00:00 2026-06-13T05:09:22+00:00

Trying to read data from a TcpClient buffer into a byte array: private byte[]

  • 0

Trying to read data from a TcpClient buffer into a byte array:

private byte[] excessData;
public bool receiveMessage(ref string recvStr, string daddy = "")
{
    recvStr = "";
    CSocket_PacketProtocol packprot = new CSocket_PacketProtocol(0, this);

    WriteDebugWithTimestamp("receiveMessage obtaining client NetworkStream - daddy = " + daddy);
    NetworkStream nStream = client.GetStream();
    WriteDebugWithTimestamp("receiveMessage client NetworkStream obtained, entering receive loop");

    int bytesRead = 0;
    do
    {
        if (!packprot.done)
        {
            try
            {
                byte[] inStream = new byte[10025]; //We'll read into this stream

                int buffSize = client.ReceiveBufferSize;
                WriteDebugWithTimestamp(String.Format("Starting nStream.Read"));
                WriteDebugWithTimestamp(String.Format(String.Format("buffSize={0} -- inStream.Length={1}", buffSize, inStream.Length)));

                bytesRead = nStream.Read(inStream, 0, buffSize); //Recieve data from stream to inStream, return amount of bytes read

                WriteDebugWithTimestamp(String.Format("receiveMessage bytesread: {0}", bytesRead.ToString()));
                WriteDebugWithTimestamp(String.Format("receiveMessage client.Connected: {0}", client.Connected.ToString()));

                ../Cut

Where client is a System.Net.Sockets.TcpClient. The exception occurs at nStream.Read.

Output when run by Debian-6.0_64 / Mono 2.11.2 as Mono --runtime=v4.0.30319 Program.exe:

receiveMessage obtaining client NetworkStream - daddy = 
receiveMessage client NetworkStream obtained, entering receive loop
receiveMessage this.excessData == null: True -- excessdata.length=n/a
Starting nStream.Read
buffSize=87380 -- inStream.Length=10025

System.ArgumentOutOfRangeException: Argument is out of range.
Parameter name: offset+size exceeds the size of buffer
  at System.Net.Sockets.NetworkStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00000] in <filename unknown>:0 
  at SocketOperations.CSocketOperations.receiveMessage (System.String& recvStr, System.String daddy) [0x00000] in <filename unknown>:0 
  at SocketServerV3_Demo.CDownloadServer_ClientHandle..ctor (System.Net.Sockets.TcpClient client) [0x00000] in <filename unknown>:0 
  at SocketServerV3_Demo.Program.handleNewConnection (System.Net.Sockets.TcpClient client) [0x00000] in <filename unknown>:0 

The buffSize is way bigger than the byte array length, but that exception doesn’t occur when I run the exact same program on windows (without mono, haven’t tried with). What gives? And how do I fix this? Is it as simple as increasing the buffer size or is there something else going on? The reason I’m asking (rather than just trying it) is because I haven’t got a clue what’s going on and I want to make sure I get it done correctly.

I’d like to find out the reason behind this behaviour, because seeing it like this I’d expect it to crash on windows as well. Unless the size of the buffer is determined by the OS and on Windows it’s smaller/equal to the byte array size.. can anyone explain?

  • 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-13T05:09:23+00:00Added an answer on June 13, 2026 at 5:09 am

    Looks like Mono’s TcpClient.ReceiveBufferSize is larger than on Windows.

    According to the MSDN Docs, the default is 8192 bytes.

    I just checked Mono’s implementation and it’s calling GetSocketOption() on the underlying socket, so it’s using the operating system’s default.

    You have three options:

    1. Change the socket’s receive buffer size by using client.ReceiveBufferSize = new_size.
    2. Allocate a large enough buffer.
    3. Don’t read more data than the buffer can hold:

      var max_size = Math.Min (client.ReceiveBufferSize, buffer.Length);
      bytesRead = nStream.Read(inStream, 0, max_size);
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to read some data from a binary file into a buffer allocated
I'm trying to read data from a binary file and put it into a
I am trying to read some data from html forms and insert it into
Presently I am trying to read the pixel data from the frame Buffer in
Im trying to read data from a text file and loading it into a
I am trying to read data from the given web page using Java. public
I am trying to read data from Excel file into my windows application. Connection
I'm trying to read data from an xml file and display it in a
I'm trying to read data from excel using POI. How can I check if
I am trying to read data from a sqlite database in an ios app.

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.