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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:51:10+00:00 2026-05-27T01:51:10+00:00

I managed to send binary data through TCP using NetworkStream and Sockets. My only

  • 0

I managed to send binary data through TCP using NetworkStream and Sockets.
My only problem is that when I send larger files like few hundred KB images, they don’t get transferred correctly, the half of the image is missing. On the server side I Socket.Send to transfer data. On the client side I use Socket.BeginReceive with a 1024 sized buffer that’s getting written into a MemoryStream, later I use new Bitmap(Stream) to convert that MemoryStream into an image that can be displayed in a PictureBox.

What method should I use to avoid data loss?

Edit: code posted

            listener = new TcpListener(myAddress, 86);
            listener.Start();
            TcpClient client = listener.AcceptTcpClient();
            ns = client.GetStream();
            byte[] buffer = new byte[1024];
            while (fileTransfer)
            {
                ms = new MemoryStream();
                do
                {
                    int length = ns.Read(buffer, 0, buffer.Length);
                    ms.Write(buffer, 0, length);
                    // with this line added the data loss apparently disappears
                    System.Threading.Thread.Sleep(1);
                } while (ns.DataAvailable);
                UpdateData();
                ms.Dispose();
                System.Threading.Thread.Sleep(10);
            }
            ns.Dispose();
            client.Close();
            listener.Stop();

Edit: the data is still corrupt sometime even with the sleep method.

  • 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-27T01:51:10+00:00Added an answer on May 27, 2026 at 1:51 am

    OK I solved it by sending the size of the data before sending the actual data. The reason it didn’t work that I wrongly assumed that the transfer was immediate. That’s why the Sleep() method in the receiver loop made a difference but did NOT solve the problem. I understand that I can’t rely on DataAvailable in this context. Even checking the length of the received data won’t work because the client might send data after the loop has finished. Checking the file size (if the data is bigger than the buffer on the receiver side) is the best solution I found so long.

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

Sidebar

Related Questions

I had Managed SMTP server configurations in IIS to send email through asp as
My newbieness is shining through here...I managed to piece together a form mailer that
I'm building a client and a server program that exchanges data over TCP and
I am trying to send an image over UDP using JAVA. I have managed
I'm using MGTwitterEngine for an iPhone app. I've managed to get it to send
I'd like to encourage our users of our RCP application to send the problem
I have managed to send out (and receive) binary SMSs, but what I want
I am trying to send touch events to a device using AndroidDebugBridge, so that
Need to send some data between managed c# and unmanaged c++. After some research
I am using the Exchange Services Managed API.... I am able to send emails

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.