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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:32:20+00:00 2026-05-19T17:32:20+00:00

I appologize for this lengthy post. I have made it as small as possible

  • 0

I appologize for this lengthy post. I have made it as small as possible while still conveying the problem.

Ok this is driving me crazy. I have a client and a server program, both in C#. The server sends data to the client via Socket.Send(). The client receives the data via Socket.BeginReceive and Socket.Receive. My pseudo-protocol is as follows: The server sends a two-bye (short) value indicating the length of the actual data followed immediately by the actual data. The client reads the first two byes asynchronously, converts the bytes to a short, and immediately reads that many bytes from the socket synchronously.

Now this works fine for one cycle every few seconds or so but when I increase the speed, things get weird. It seems that the client will randomly read the actual data when it is attempting to read from the two-byte length. It then tries to convert these arbitrary two bytes into a short which leads to a completely incorrect value, causing a crash. The following code is from my program but trimmed to only show the important lines.

Server-side method for sending data:

private static object myLock = new object();
private static bool sendData(Socket sock, String prefix, byte[] data)
{
    lock(myLock){
        try
        {
            // prefix is always a 4-bytes string
            // encoder is an ASCIIEncoding object    
            byte[] prefixBytes = encoder.GetBytes(prefix);
            short length = (short)(prefixBytes.Length + data.Length);

            sock.Send(BitConverter.GetBytes(length));
            sock.Send(prefixBytes);
            sock.Send(data);

            return true;
        } 
        catch(Exception e){/*blah blah blah*/}
    }
}

Client-side method for receiving data:

private static object myLock = new object();
private void receiveData(IAsyncResult result)
{
    lock(myLock){
        byte[] buffer = new byte[1024];
        Socket sock = result.AsyncState as Socket;
        try
        {
            sock.EndReceive(result);
            short n = BitConverter.ToInt16(smallBuffer, 0);
            // smallBuffer is a 2-byte array

            // Receive n bytes
            sock.Receive(buffer, n, SocketFlags.None);

            // Determine the prefix.  encoder is an ASCIIEncoding object
            String prefix = encoder.GetString(buffer, 0, 4);

            // Code to process the data goes here

            sock.BeginReceive(smallBuffer, 0, 2, SocketFlags.None, receiveData, sock);
        }
        catch(Exception e){/*blah blah blah*/}
    }
}

Server-side code to recreate the issue reliably:

byte[] b = new byte[1020];  // arbitrary length

for (int i = 0; i < b.Length; i++)
    b[i] = 7;  // arbitrary value of 7

while (true)
{
    sendData(socket, "PRFX", b);
    // socket is a Socket connected to a client running the same code as above
    // "PRFX" is an arbitrary 4-character string that will be sent
}

Looking at the code above, one can determine that the server will forever send the number 1024, the length of the total data, including the prefix, as a short( 0x400) followed by “PRFX” in ASCII binary followed by a bunch of 7’s (0x07). The client will forever read the first two bytes (0x400), interpret that as being 1024, store that value as n, and then read 1024 byes from the stream.

This is indeed what it does for the first 40 or so iterations but, spontaneously, the client will read the first two byes and interpret them as being 1799, not 1024! 1799 in hex is 0x0707 which is two consecutive 7’s!!! That is the data, not the length! What happened to those two bytes? This happens with whatever value I put in the byte array, I just chose 7 because it is easy to see the correlation with 1799.

If you are still reading by this point, I applaud your dedication.

Some important observations:

  • Decreasing the length of b will increase the number of iterations before the problem occurs but does not prevent the problem from occurring
  • Adding a significant delay between each iteration of the loop may prevent the problem from occurring
  • This DOES NOT happen when using both the client and server on the same host and connecting via a loopback address.

As mentioned, this is driving me crazy! I am always able to solve my programming problems but this one has completely stumped me. So I am here pleading for any advise or knowledge on this subject.

Thank you.

  • 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-19T17:32:20+00:00Added an answer on May 19, 2026 at 5:32 pm

    I suspect you’re assuming that the entire message is delivered in one call to receiveData. This is not, in general, the case. Fragmentation, delays, etc can end up meaning that the data arrives in dribbles, so that you might get your receiveData function called when there’s only, e.g. 900 bytes ready. Your call sock.Receive(buffer, n, SocketFlags.None); says “Give me the data into the buffer, up to n bytes” – you might receive fewer, and the number of bytes actually read is returned by Receive.

    This explains why decreasing b, adding more delay or using the same host seems to “fix” the problem – the odds are vastly increased that the entire message will arrive in one go using these methods (smaller b, smaller data; adding a delay means there’s less overall data in the pipe; there’s no network in the way for the local address).

    To see if this is indeed the problem, log the return value of Receive. It will occasionally be less than 1024 if my diagnosis is correct. To fix it, you need to either wait until the local network stack’s buffer has all your data (not ideal), or just receive data as and when it arrives, storing it locally until a whole message is ready and processing it then.

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

Sidebar

Related Questions

Note: Let me appologize for the length of this question, i had to put
I apologise in advance if this question isn't very specific. Would it be possible
First of all, after testing a while I have to say, stackoverflow is really
I have written this T-SQL script to roll up duplicate rows in a database
I apologize if this is a basic question, however I have been searching on
I'm having a problem similar to the one discussed in this thread , but
I try hard to find the problem in this Java code, but I can't
I have spent many hours working on this program that is supposed to play
I apologize if this is slightly off-topic. I'm hoping to find a software-as-a-service CRM
First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS

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.