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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:27:34+00:00 2026-06-07T07:27:34+00:00

To put my toe in the water of Network programming, I wrote a little

  • 0

To put my toe in the water of Network programming, I wrote a little Console App to send a png file to a server (another console app). The file being written by the server is slightly bigger than the source png file. And it will not open.

The code for the client app is:

    private static void SendFile()
    {
        using (TcpClient tcpClient = new TcpClient("localhost", 6576))
        {
            using (NetworkStream networkStream = tcpClient.GetStream())
            {
                //FileStream fileStream = File.Open(@"E:\carry on baggage.PNG", FileMode.Open);

                byte[] dataToSend = File.ReadAllBytes(@"E:\carry on baggage.PNG");

                networkStream.Write(dataToSend, 0, dataToSend.Length);
                networkStream.Flush();

            }
        }

    }

The code for the Server app is :

    private static void Main(string[] args)
    {
        Thread thread = new Thread(new ThreadStart(Listen));
        thread.Start();

        Console.WriteLine("Listening...");
        Console.ReadLine();
    }

    private static void Listen()
    {
        IPAddress localAddress = IPAddress.Parse("127.0.0.1");
        int port = 6576;
        TcpListener tcpListener = new TcpListener(localAddress, port);
        tcpListener.Start();

        using (TcpClient tcpClient = tcpListener.AcceptTcpClient())
        {
            using (NetworkStream networkStream = tcpClient.GetStream())
            {
                using (Stream stream = new FileStream(@"D:\carry on baggage.PNG", FileMode.Create, FileAccess.ReadWrite))
                {
                    // Buffer for reading data
                    Byte[] bytes = new Byte[1024];
                    var data = new List<byte>();

                    int length;

                    while ((length = networkStream.Read(bytes, 0, bytes.Length)) != 0)
                    {
                        var copy = new byte[length];
                        Array.Copy(bytes, 0, copy, 0, length);
                        data.AddRange(copy);
                    }

                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    stream.Position = 0;
                    binaryFormatter.Serialize(stream, data.ToArray());
                }
            }

        }
        tcpListener.Stop();

The size of the written file is 24,103Kb, whereas the source file is only 24,079Kb.

Is it apparent to anyone why this operation is failing?

Cheers

  • 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-07T07:27:36+00:00Added an answer on June 7, 2026 at 7:27 am

    You are writing your output using a BinaryFormatter. I’m pretty sure that this will add some bytes at the start of the output to indicate the type that you’re outputting (in this case System.Byte[]).
    Just write the bytes out directly to the file without using the formatter:

    using (Stream stream = new FileStream(@"D:\carry on baggage.PNG", FileMode.Create, FileAccess.ReadWrite))
    {
        // Buffer for reading data
        Byte[] bytes = new Byte[1024];
    
        int length;
    
        while ((length = networkStream.Read(bytes, 0, bytes.Length)) != 0)
        {
            stream.Write(bytes, 0, length);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

put filename will put the file to cureent directory in server. Which command i
I have made a simple tic tac toe app for Android which needs no
After upgrading my PHP to 5.4.3 (WAMP server 2.2), my web app made in
Put simply, I want to subtract one array from another. The arrays are arrays
Put another way, you create a new kind of object by making a package
I am writing an iPad app and want to put an option on the
Put it another way: what code have you written that cannot fail. I'm interested
Put them all in one separate folder structure or along with classes which implements
Put simply: it causes a massive memory leak within an hour if I work
Put together an extremely simple Knockout.js exmaple to learn how it works. I immediately

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.