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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:13:49+00:00 2026-05-28T14:13:49+00:00

I am trying my hands on Socket programming in C# (.NET). I have referred

  • 0

I am trying my hands on Socket programming in C# (.NET). I have referred to a couple of examples from codeproject, but my file transfer always sends out incomplete data.

Data of very very small sizes works fine like 1kB. But larger files get transferred incomplete, specifically images.

Here’s my client code, that sends a file to the server. The clientData stores the binary of the file.

byte[] fileData = File.ReadAllBytes(filePath + fileName);
            byte[] clientData = new byte[4 + fileNameByte.Length + fileData.Length];
            byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);

            fileNameLen.CopyTo(clientData, 0);
            fileNameByte.CopyTo(clientData, 4);
            fileData.CopyTo(clientData, 4 + fileNameByte.Length);

            curMsg = "Connection to server ...";
            clientSock.Connect(ipEnd);
            int count;
            curMsg = "File sending...";
            count = clientSock.Send(clientData);
            MessageBox.Show(count+" "+clientData.Length);

            curMsg = "Disconnecting...";
            clientSock.Close();

While here is my code for the server, that receives the file.

                sock.Listen(100);

            curMsg = "Running and waiting to receive file.";
            Socket clientSock = sock.Accept();

            byte[] clientData = new byte[1024 * 5000];

            int receivedBytesLen = clientSock.Receive(clientData);
            curMsg = "Receiving data...";

            int fileNameLen = BitConverter.ToInt32(clientData, 0);
            string fileName = Encoding.ASCII.GetString(clientData, 4, fileNameLen);

            BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath +"/"+ fileName, FileMode.Append)); ;
            bWrite.Write(clientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen);

            curMsg = "Saving file...";

            bWrite.Close();
            clientSock.Close();

What is funny is, that when I set a break-point at ‘send’ on the client, the file gets transferred fine. I am observing the byte counts at both ends and the transfer bytes match when I use a break-point, while when I don’t the received bytes are lower than transferred bytes at client.

What am I doing wrong?

  • 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-28T14:13:50+00:00Added an answer on May 28, 2026 at 2:13 pm

    You forgot to implement a protocol! To use TCP, you have to design and implement a protocol that defines what bytes will be sent and how the receiver will identify messages or protocol data elements. You haven’t done any of this. Your receive code has no way to know whether it has received the entire file or not because there’s no protocol to tell it nor any code to implement such a protocol. So of course it gets it wrong — it has no way to get it right.

    If receivedBytesLen is 1, your code fails horribly.

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

Sidebar

Related Questions

I am trying to get my hands on MVC. I am from ASP.Net background.
So I'm trying my hands at a css menu and I have a simple
Lately I have been trying my hands on Eclipse IDE for java development. I
I have a mystery on my hands. I am trying to learn managed C++
I am trying my hands on sample codes from a book, and so I
I am trying my hands on asp.net+ajax+httpmodule. My Form <form id=LoginForm runat=server> <asp:ScriptManager ID=LoginScriptMgr
I am trying my hands on Visual Studio 2010 (Asp.Net 4.0) for the first
I am trying my hands on lettuce with splinter. I have a django app
I have been trying my hands on opensource tool JUnit for testing Java Code
I am trying to get my hands on learning Visual force. I have an

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.