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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:45:24+00:00 2026-05-31T00:45:24+00:00

I have problem with my transfer files application .. everything looks great and works

  • 0

I have problem with my transfer files application .. everything looks great and works the way it should be .. but then I discovered that some pieces of the sent file is corrupted.

Like when I send a video file (.MP4) it sent successfully but when I play it some frames of it got corrupted like in the snapshot picture .. I played the source Mp4 file and the picture was so perfect and never looked like in that snapshot picture.

So here’s my code .. I’m confused now about using fs.Seek .. someone told me it doesn’t make the new file the same as the source file.

I’m using SOCKETS TCP .. (.Net 4.0)

enter image description here

Send method (CLIENT):

FileStream fs;
long sum;
long fileSize;
byte[] data = null;
NetworkStream network; 
const int packetSize = 1024*8;

private void SendFile(string srcPath, string destPath)
    {
        string dest = Path.Combine(destPath, Path.GetFileName(srcPath));
        using (fs = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
        {
            try
            {
                sum = 0;
                int count = 0;
                data = new byte[packetSize];
                //send the destination path and the file size to SERVER.
                SendCommand("receive<" + dest + "<" + fs.Length.ToString());
                while (sum < fileSize)
                {
                    if (fileSize - sum < packetSize)
                    {
                        count = fs.Read(data, 0, Convert.ToInt32(fileSize - sum));
                        network.Write(data, 0, Convert.ToInt32(fileSize - sum));
                    }
                    else
                    {
                        count = fs.Read(data, 0, data.Length);
                        network.Write(data, 0, data.Length);
                    }
                    fs.Seek(sum, SeekOrigin.Begin);
                    sum += count;
                    backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
                }
                network.Flush();
            }
            finally
            {
                fs.Dispose();
                data = null;
            }
        }
    }

Receive method (SERVER):

FileStream fs;
long sum;
long fileSize;
byte[] data = null;
NetworkStream network; 
const int packetSize = 1024*8;

public void Receive(string destPath, long fileSize)
    {
        using (fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
        {
            try
            {
                int count = 0;
                long sum = 0;
                data = new byte[packetSize];
                while (sum < fileSize)
                {
                    if (fileSize - sum < packetSize)
                    {
                        count = network.Read(data, 0, Convert.ToInt32(fileSize - sum));
                        fs.Write(data, 0, Convert.ToInt32(fileSize - sum));
                    }
                    else
                    {
                        count = network.Read(data, 0, data.Length);
                        fs.Write(data, 0, data.Length);
                    }
                    fs.Seek(sum, SeekOrigin.Begin);
                    sum += count;
                }
            }
            finally
            {
                fs.Dispose();
                data = null;
            }
        }
    }
  • 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-31T00:45:26+00:00Added an answer on May 31, 2026 at 12:45 am

    In case you didn’t solve it by now, I propose this code. Put it inside while (sum < fileSize).

    Send:

        count = fs.Read(data, 0, packetSize);
        network.Write(data, 0, count);
        sum += count;
        backgroundWorker1.ReportProgress((int)((sum * 100) / fileSize));
    

    and Receive:

        count = network.Read(data, 0, packetSize);
        fs.Write(data, 0, count);
        sum += count;
    

    This will ensure that you transfer just the data you have read. Notice that you do not need if part because Read will return number of bytes read, so it doesn’t matter if you are at beginning or at end of the file, Read will read all it can up to limit you provided as parameter.

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

Sidebar

Related Questions

I have a PHP file that serves up a file, but the problem is
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with fancybox. I want to write a function that will run
I have an application that processes file transfers. In some instances, I need to
In my application, i have one exe file that will do some conversion on
I'm working at Windows Phone application these days and have faced the problem with
I have a desktop (winforms) application that uses a Firebird database as a data
I have an application that connects to a database and can be used in
I have a little problem, I am building a database from CSV files using
I have a Python application (command line tool running on a machine M1) that

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.