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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:29:02+00:00 2026-06-06T00:29:02+00:00

I have a client server situation, where the client sends the data (a movie

  • 0

I have a client server situation, where the client sends the data (a movie for example) to the server, the server saves that data to the HDD.

It sends the data by a fixed array of bytes. After the bytes are sent, the server asks if there is more, if yes, send more and so on. Every thing is going well, all the data gets across.

But when I try to play the movie, it cant be played and if I look to the file length of each movie (client and server) the server movie is bigger then the client movie.also when I look at the command screen at the end of the sending/receiving data there is more then a 100% of the bytes that are across.

The only thing I can think of that can be wrong is the fact that my server reads in the stream till the fixed buffer array is full and therefor has at the end more bytes then the client. However if that is the problem how can I solve this?

I’ve just added the 2methods of sending, because the tcp connection works, any help is welcome.

Client

public void SendData(NetworkStream nws, StreamReader sr, StreamWriter sw)
{
    using (FileStream reader = new FileStream(this.path, FileMode.Open, FileAccess.Read))
    {                
         byte[] buffer = new byte[1024];
         int currentBlockSize = 0;

         while ((currentBlockSize = reader.Read(buffer, 0, buffer.Length)) > 0)
         {
             sw.WriteLine(true.ToString());
             sw.Flush();
             string wait = sr.ReadLine();
             nws.Write(buffer, 0, buffer.Length);
             nws.Flush();
             label1.Text = sr.ReadLine();
         }
         sw.WriteLine(false.ToString());
         sw.Flush();                
    }
}

Server

    private void GetMovieData(NetworkStream nws, StreamReader sr, StreamWriter sw, Film filmInfo)
    {
        Console.WriteLine("Adding Movie: {0}", filmInfo.Titel);
        double persentage = 0;

        string thePath = this.Path + @"\films\" + filmInfo.Titel + @"\";
        Directory.CreateDirectory(thePath);

        thePath += filmInfo.Titel + filmInfo.Extentie;

        try
        {
            byte[] buffer = new byte[1024]; //1Kb buffer

            long fileLength = filmInfo.TotalBytes;
            long totalBytes = 0;

            using (FileStream writer = new FileStream(thePath, FileMode.CreateNew, FileAccess.Write))
            {
                int currentBlockSize = 0;
                bool more;
                sw.WriteLine("DATA");
                sw.Flush();
                more = Convert.ToBoolean(sr.ReadLine());


                while (more)
                {
                    sw.WriteLine("SEND");
                    sw.Flush();
                    currentBlockSize = nws.Read(buffer, 0, buffer.Length);
                    totalBytes += currentBlockSize;
                    writer.Write(buffer, 0, currentBlockSize);

                    persentage = (double)totalBytes * 100.0 / fileLength;
                    Console.WriteLine(persentage.ToString());

                    sw.WriteLine("MORE");
                    sw.Flush();
                    string test = sr.ReadLine();
                    more = Convert.ToBoolean(test);                       
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
  • 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-06T00:29:06+00:00Added an answer on June 6, 2026 at 12:29 am

    There is a reason why Read() returns the number of bytes read: it’s possible it will return less than the size of the buffer. Because of this, you should do something like nws.Write(buffer, 0, currentBlockSize); in SendData(). But this will break your protocol, because the blocks won’t have the size anymore.

    But I find it hard to believe your code actually behaves the way you describe. That’s because Read() in GetMovieData() also may not fill the whole buffer. Also, StreamReader is allowed to keep some data in an internal buffer, which would mean you could read some completely bogus data.

    I think code like this, where you’re combining Streams and StreamReaders/StreamWriters is a really bad idea. It would be hard to make it actually correct. What you should do instead is to make your protocol completely byte-based (not character-based), even if those bytes are ASCII-encoded "SEND".

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

Sidebar

Related Questions

We have client server based app which saves user related data into a zip
Common situation: I have a client on my server who may update some of
I have this situation.... Client-initiated SOAP 1.1 communication between one server and let's say,
If I have a Server/Client application that both reference the same DLL which contains
Here is the situation: I have a huge data set that I need quick
I have a client/server application written in C#/.NET 3.5 that I want to do
I have to write simple client-server application that uses Unix datagram socket. Client may
I have client/server applications and a very simple protocol for communication. More precisely, it's
I have Client-Server environment and developed a project for Client-Server. I need to share
i have client-server app, I'm managing connections with Threads and handlers inside app, but

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.