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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:52:46+00:00 2026-06-13T04:52:46+00:00

Simple situation: The client on a Socket sends a pieces(e.g., 256 byte) of the

  • 0

Simple situation:

The client on a Socket sends a pieces(e.g., 256 byte) of the file (data) in the format byte []to the server. The server receives the data asynchronously.
How to determine when a file (data) is transmitted completely? (Server-side)

Here’s the code on server-side responsible for receiving data

public static void ReadCallback(IAsyncResult ar)
{
        String content = String.Empty;

        // Retrieve the state object and the handler socket
        // from the asynchronous state object.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;


        // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0)
        {

            BinaryWriter writer = new BinaryWriter(File.Open(@"D:\test.png", FileMode.Append));

            writer.Write(state.buffer, 0, bytesRead);
            writer.Close();

            // All the data has been read from the 
            // client. Display it on the console.
            Console.WriteLine("Read {0} bytes from socket!",
                bytesRead);

            handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
        }
}

Is there a method that allows to make the some following?

if (bytesRead > 0)
{

     ....
     if(state.buffer!=end of receive)
     {
     handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
         new AsyncCallback(ReadCallback), state);
     }

}

Or, I may to try add some information to the this byte[] object (e.g., some string with <EOF> tag)
but I must to analyse this info on each step.
May I do this check more simple and how? Or use another way …

  • 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-13T04:52:47+00:00Added an answer on June 13, 2026 at 4:52 am

    The only way to do it is to sent a header (of specific size) in front of every message. So, the each message should consist of header and body. The data stream should look like this:

    [HEADER][BODY][HEADER][SOME BIGGER BODY][HEADER][SOME EXTRA BIG BODY]

    As I said, the header should be of specific size and should contain some custom service fields inlcuding the size of the message’s body in bytes. In your case the header could contain only body size, i.e. int value (4 bytes). The receive process should look like this:

    1. Receive 4 bytes (header);
    2. Retrieve body size from header (just convert header to int);
    3. Receive the number of bytes specified in header (i.e. receive message body);
    4. Handle message body;
    5. Go to 1.

    I know, it may seem complicated for you, but it is the common way to do it. But you can simplify the code by using Rx library. After implementing some extensions methods for socket (WhenReadExact, the implementation could easily be found over Internet, for example here), the code will look like this:

    var whenFileReceived = from header in socket.WhenReadExact(4)
                           let bodySize = BitConverter.ToInt32(header)
                           from body in socket.WhenReadExact(bodySize)
                           select body;
    
    whenFileReceived.Subscribe(
        file =>
        {
            // Handle file here
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a simple client/server file synchronization . For this, client and
I have a simple UDP socket program in C. The client transmits data to
I have to write simple client-server application that uses Unix datagram socket. Client may
My situation is simple. I have this in my program: File folder = new
First let me set the situation. I am writing a simple client html page
I'm trying to make a simple client & server messaging program in python, and
I've a producer/consumer set-up: Our client is giving us data that our server processes,
I have been working on a (relatively) simple tcp client/server chat program for my
I have a simple situation here. lets face html code first => <form name=geoKey
I just stuck in a very simple situation bt couldnt help myself.My question is

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.