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

  • Home
  • SEARCH
  • 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 7169597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T15:01:50+00:00 2026-05-28T15:01:50+00:00

I need to write a simple file transfer protocol in C++ to upload or

  • 0

I need to write a simple file transfer protocol in C++ to upload or download a file from a client to a server. Right now both my client and server applications are do-it-all scripts. I want to modularize everything in classes/methods so I can more easily modify things later.

To upload a file, I need the name of user sending file (no authentication though), file name, file size, and file contents. This is what I have on client side:

int sendFile(string fileName, SOCKET s) {
    ifstream file;
    int ibytesent;
    int size;
    char fileSize[packetSize];

    // Open file, set get pointer at end of file
    file.open (fileName.c_str(), ios::binary | ios::ate);

    // Get file size and pass it to buffer
    size = (int) file.tellg();

    //itoa(size, fileSize, packetSize);
    sprintf(fileSize, "%d", size);

    // Set pointer to beginning
    file.seekg(0, ios::beg);

    // Send file size
    if (send(s, fileSize, packetSize, 0) == SOCKET_ERROR) {
        throw "Send failed\n";
    }

    cout << "Size sent: " << size;

    // Buffer the file Name
    char fileNameBuffer[packetSize];

    sprintf(fileNameBuffer, fileName.c_str());

    // Send the file name
    if (send(s, fileNameBuffer, packetSize, 0) == SOCKET_ERROR) {
        throw "Send failed\n";
    }

    cout << "File name sent: " << fileName << endl;

    char buf[packetSize];
    ibytesent = 0;
    // Loop until the whole file is sent
    while(file.good()) {
        // Read next packetSize bytes into buf
        file.read(buf, packetSize);

        // Send the read data
        ibytesent += send(s, buf, packetSize, 0);

        if (ibytessent == SOCKET_ERROR) {
            throw "Send failed\n";
        }
    }

    cout << "File sent.\n";
    //wait for reception of server response.
    ibytesrecv=0;
    /*
    if((ibytesrecv = recv(s,szbuffer,128,0)) == SOCKET_ERROR)
        throw "Receive failed\n";
    else
        cout << "File received.";
     */
    file.close();

    return 1;
}

And then there is a main() method that opens up a socket and connects to the server. My question really is about how to design/implement a proper file transfer protocol. What is a good structure for it? I would also like to know your opinions about how to split (and send) a file into a packet structure rather than the way I do it now. I don’t necessarily need code, but separation of concerns into this and that class, etc.

Please don’t recommend an already existing protocol since that isn’t the point of this exercise.

Thanks

EDIT I kind of found something similar to what I’m looking for here: http://www.eventhelix.com/realtimemantra/PatternCatalog/protocol_layer.htm

  • 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-28T15:01:52+00:00Added an answer on May 28, 2026 at 3:01 pm

    Here’s a take on your possible classes:

    • ISender and IReceiver interfaces for senders and receivers

    • StreamSender and StreamReceiver concrete implementations of ISender and IReceiver respectively (using sockets I guess)

    • IDataSource interface to get data from source

    • FileReader a concrete implementation of IDataSource to read file off disk to be fed into StreamSender

    Example:

    ISender sender=new StreamSender(new FileReader("filename.txt"));
    sender.setDest(new StreamReceiver(host));
    sender.setName(senderName);
    sender.send();
    

    Obviously this is just an example off the top of my head. There will be missing pieces.

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

Sidebar

Related Questions

I need to write a 'simple' util to convert from ASCII to EBCDIC? The
I need to write a simple program that records all the input from parallel
I need to move or copy a simple text file from one web site
I need to write a simple terminal-based program that should, Read some text from
I need to write the data from an Excel sheet/workbook to the text file
Simply I need to write echo t${count} = $t${count} To a text file, including
As a part of our application I need to write simple workflow system which
I need to write a simple command-line application in Java. It would be nice
I need to write a simple parser to a sort of Domain Specific Language.
I need to write a simple program for work that does the following: read

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.