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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:25:52+00:00 2026-05-27T22:25:52+00:00

i have this function to get the content of file , #define BUFSIZE 512

  • 0

i have this function to get the content of file ,

#define BUFSIZE 512

vector<int> getContFile(char* pFile) {
        ifstream vCin(pFile, ios::binary);
        ifstream::pos_type size;
        // get vLength of file:
        vCin.seekg(0, ios::end);
        size = vCin.tellg();
        vCin.seekg(0, ios::beg);
        vector<int> vTmp;
        for (int i = 0; i < size; i++)
            vTmp.push_back(vCin.get());
        vCin.close();
        return vTmp;
    }

and this to send to the server

 void SendFile() {
        SendS("upFileUser");
        int i;
        vector<int> vTmp = getContFile("/usr/home/alex/Desktop/eval.tar");
        for (i = 0; i < vTmp.size(); i += BUFSIZE) {
            char *vBuff = new char[BUFSIZE];
            for (int j = i; j < BUFSIZE; j++)
                vBuff[j] = (char(vTmp[i]));
            SendS(vBuff);
            }
        if (i < (vTmp.size() - 1)) {
            char *vBuff = new char[vTmp.size() - i];
            for (int j = 0; j < vTmp.size() - i; j++)
                vBuff[j + i] = (char(vTmp[j + i]));
            SendS(vBuff);
        }
        sendS("endOfFileTransmision");
    }

    void SendS(char* pSir) {
        int vLen = strlen(pSir);
        write(pSocket, &vLen, sizeof (int));
        write(pSocket, pSir, vLen);
    }

this is the receve function

 char* reciveS() {
        char* vTmp;
         int vCt = 0;
        read(pSocket, &vCt, sizeof (vCt));
        if (vCt != 0) {
            vTmp = new char[vCt];
            read(vSocket, vTmp, vCt);
        } else {
            vTmp = NULL;    
        }
        return vTmp;
    } 

bool receveFile(void) {
 char* vReceve = reciveS();
    if (strcmp(vReceve, "upFileUser") == 0)
{
    ofstream vCoutFile;
    vCoutFile.open("data2.tar", ios::out | ios::binary);
    while (true) {
        char *vTmp = new char[BUFSIZ];
        vTmp = reciveS();
        cout<<vTmp;
        if (strcmp(vTmp, "endOfFileTransmision") == 0) break;
        else {
            cout << vTmp;
            vCoutFile << vTmp;
        }
    }
    vCoutFile.close();
}
}

and the result are a broke pipe(i run this to freebsd 6.4 amd with g++ compiler) , so what i miss , the connection are good i can transfer text from client to server and reverse the problem are with binary file

  • 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-27T22:25:52+00:00Added an answer on May 27, 2026 at 10:25 pm

    I see two problems with your code:

    1. You are making a lot of allocations (new) but you never free the memory.
    2. In the SendS function you are taking the string length, but the data in that “string” is from a vector of integers and is binary. This means that the data can contain the string-terminating '\0' character (the integer 0).

    Besides that, I really don’t follow what you are doing. Instead of reading into a vector, create a char-buffer and allocate enough memory to put the whole file into that buffer (char *buffer = new char[length_of_file]) and send it, with the length of the buffer first.

    Something like this:

    std::pair<size_t, char *> getContFile(const char *pFile)
    {
        ifstream vCin(pFile, ios::binary);
        ifstream::pos_type size;
    
        vCin.seekg(0, ios::end);
        size = vCin.tellg();
        vCin.seekg(0, ios::beg);
    
        char *buffer = new char[size];
    
        vCin.read(buffer, size);
    
        return std::make_pair(static_cast<size_t>(size), buffer);
    }
    
    void SendFile()
    {
        SendS("upFileUser", strlen("upFileUser"));
        std::pair<size_t, char *> vTmp = getContFile("/usr/home/alex/Desktop/eval.tar");
    
        SendS(vTmp.second, vTmp.first);
    
        delete [] vTmp.second;
    }
    
    void SendS(char *buffer, size_t length)
    {
        // Send the length
        size_t tmp = htonl(length);
        write(pSocket, &tmp, sizeof(tmp));
    
        // Send the buffer
        while (length > 0)
        {
            ssize_t sent = write(pSocket, buffer, length);
            if (sent <= 0)
            {
                // Some kind of error
                break;
            }
    
            buffer += sent;
            length -= sent;
        }
    }
    

    Do something similar on the receiving side.

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

Sidebar

Related Questions

I have progress.js file which has the following code $('#text_area_input').keyup(function() { var text_area_box =$(this).val();//Get
I have this function in my model the purpose of it is to get
I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {
I have this function: /*This func runs *.c1 file, and replace every include file
I have this Perl/CGI to upload files and get the uploaded file size while
Ok, I have this code: <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script> <script type=text/javascript> function get()
I have a jQuery AJAX call with type:'GET' like this: $.ajax({type:'GET',url:'/createUser',data:userId=12345&userName=test, success:function(data){ alert('successful'); }
I have this: function test() { this.method = function () { $(html).mousemove(function(event) { console.log('~>
I have this: function test1() { this.count = 0; this.active = 0; this.enable =
I have this function which removes occurrences of a given element within the list

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.