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 this piece of code: $(#faq).click(function () { var url = $.get(faq, {
I have this function to read in all ints from the file. The problem
I have this function: public bool IsValidProduct(int productTypeId) { bool isValid = false; if
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target
I have this function: /*This func runs *.c1 file, and replace every include file
I have this code in a assets controller to get images: function images($path,$image_name) {
I have this Perl/CGI to upload files and get the uploaded file size while
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 in my Javascript Code that updates html fields with their
I have this function in my head: <head> window.onload = function(){ var x =

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.