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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:24:25+00:00 2026-05-24T23:24:25+00:00

I have some pretty simple code that reads lines from a network stream that

  • 0

I have some pretty simple code that reads lines from a network stream that it connects to. In the code example only one line is every read and it doesn’t carry on getting more from the server.

What is wrong?

        byte[] readBuffer = new byte[1024];
        byte[] tempBuff = new byte[1024];
        int tempBuffSize = 0;    


private void btnConnect_Click(object sender, EventArgs e)
            {

            TcpClient tcpClient = new TcpClient("192.168.1.151", 5505);
            NetworkStream stream = tcpClient.GetStream();
            stream.BeginRead(readBuffer, 0, 1024, readHandler, tcpClient); 
        }

void readHandler(IAsyncResult result)
        {
            TcpClient tcpClient = (TcpClient)result.AsyncState;
            int dataLen = tcpClient.GetStream().EndRead(result);

            int currStart = 0;
            int currEnd = -1;

            for (int i = 0; i < dataLen; i++)
            {
                if (readBuffer[i] == '\r' && i < (readBuffer.Length - 1) &&
                    readBuffer[i + 1] == '\n')
                {
                    // Set the end of the data 
                    currEnd = i - 1;

                    // If we have left overs from previous runs: 
                    if (tempBuffSize != 0)
                    {

                        byte[] joinedData = new byte[tempBuffSize + (currEnd - currStart + 1)];
                        Array.Copy(tempBuff, 0, joinedData, 0, tempBuffSize);
                        Array.Copy(readBuffer, currStart, joinedData, tempBuffSize, (currEnd - currStart + 1));

                        System.Text.Encoding enc = System.Text.Encoding.ASCII;
                        string myString = enc.GetString(joinedData);
                        System.Diagnostics.Debug.Write(myString);

                        tempBuffSize = 0;
                    }
                    else
                    {
                        System.Text.Encoding enc = System.Text.Encoding.ASCII;
                        string myString = enc.GetString(readBuffer);
                        System.Diagnostics.Debug.Write(myString);

                        // HandleData(readBuffer, currStart, currEnd);
                    }

                    // Set the new start - after our delimiter 
                    currStart = i + 2;
                }

            }

            // See if we still have any leftovers 
            if (currStart < dataLen)
            {
                Array.Copy(readBuffer, currStart, tempBuff, 0, dataLen - currStart);
                tempBuffSize = dataLen - currStart;
            }
        }  
  • 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-24T23:24:27+00:00Added an answer on May 24, 2026 at 11:24 pm

    Why do you expect it to read the whole information in the first place? I am not an expert but it seems to me that neither the synchronous nor the asynchronous methods guarantee reading all the data (whatever that means because as long as a socket is open more data can arrive). After the code in your EndRead method you should call Read or BeginRead again if you expect more data. You should know if more data is expected based on the protocol you’ve established with the client.

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

Sidebar

Related Questions

Pretty much what the title says really. We have some code that is .NET
I have some simple OpenGL ES code in C++ that I've run on a
I have heard mention that some desktop applications are pretty much just wrappers for
Pretty simple question this time around. I have an application that communicates with another
I have the understanding that using data access routines directly from presentation code is
I have a script that appends some rows to a table. One of the
I'm doing some relatively simple I/O in Java. I have a .txt files that
I have some pretty basic code to capture a still image using AVFoundation. AVCaptureDeviceInput
I'm looking at building a Rails application which will have some pretty large tables
I have a pretty beefy development machine and three monitors, so after some intense

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.