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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T10:32:53+00:00 2026-06-03T10:32:53+00:00

I am having some trouble with a TCP Client , the service connects to

  • 0

I am having some trouble with a TCP Client , the service connects to the server and listens for messages, which are received and processed 90% of the time. After a bit of debugging I found that on some occasions I get the “Unable to read whole packet of data!” error.(in code)

I don’t have control over what the server sends.
My thinking is it is not receiving the complete message (I think it fails on longer messages but unsure due to volume).

What is the best way I can account for this?, or any other tips. I have my receive method below which is where all the action and problems are.

I am sorry this is my first attempt with TCP , so don’t be afraid

    public void Receive(IAsyncResult ar)
        {
            if (ar != null)
            {
                try
                {

                    byte[] mLenBytes = (byte[])ar.AsyncState;
                    byte[] mDataBytes = null;
                    int bytes_read = mTcpClient.Client.EndReceive(ar);
                    if (bytes_read != 4)
                        throw new Exception("Unable to read whole header!");
                    int len = mLenBytes[3] + ((mLenBytes[2] + (mLenBytes[1] + (mLenBytes[0] << 8)) << 8) << 8);
                    mDataBytes = new byte[len];
                    bytes_read = mTcpClient.Client.Receive(mDataBytes, len, SocketFlags.None);
                    if (bytes_read != len)
                        throw new Exception("Unable to read whole packet of data!" + "Expected " + len + " Got "  + Convert.ToString(bytes_read) + "\r\n" + (System.Text.Encoding.ASCII.GetString(mDataBytes)));
                        //This is the error that is raised!. 

                    // raise an event
                    PhoneBoxEventArgs e1 = new PhoneBoxEventArgs(System.Text.Encoding.UTF8.GetString(mDataBytes));
                    Console.WriteLine("Data received is = " + e1.Data);
                    OnPassEvent(e1);

                }
                catch (Exception ex)
                {

                }

            }
            byte[] mLenBytes_new = new byte[4];
            mTcpClient.Client.BeginReceive(mLenBytes_new, 0, 4, SocketFlags.None, new AsyncCallback(Receive), mLenBytes_new);
        }
  • 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-03T10:32:54+00:00Added an answer on June 3, 2026 at 10:32 am

    You can not assume to get the entire “message” in one read from the TCP server. That is usually why you do the entire header-stuff.

    The header is supposed to be 4 bytes long and tell you, how many bytes are to follow. So what you need to do is:

    1. Read asynchronously until you have 4 bytes
    2. Keep reading asynchronously until you have as many bytes as you’ve been told to receive in the header

    What you’re doing is:

    1. Assume you get 4 bytes in one go and fail if you don’t (problem: you can not assume that you get 4 bytes in one receive)
    2. Receive as many bytes as you can, fail if you didn’t get enough bytes (problem again: you can not be sure to receive all bytes in one go)

    Also I can tell from your code that you’ve started receiving asynchronously. Why do you then switch to synchronous receiveing after you got the header?

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

Sidebar

Related Questions

Im having some trouble with an oracle database. Every time i try to connect,
I having some trouble figuring this problem out where I have one button which
After having some trouble with object deletion, I ran a simple test: Created a
Having some trouble using basic authentication with a simple test Wcf service. I am
Having some trouble figuring out why my ImageView buttons stop working after I change
I´m having some trouble deciding in which way to go... I've got this application
I am writing a prototype TCP connection and I am having some trouble homogenizing
I am having some trouble with compiling my code due to these error messages
After having some trouble with setting up a thread to start my MIDI sequencer
Having some trouble with R's garbage collection, when passing objects to C++. We have

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.