xnl96
xnl96

Reputation: 189

c++ socket binary file

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

Upvotes: 0

Views: 315

Answers (1)

Some programmer dude
Some programmer dude

Reputation: 409196

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.

Upvotes: 1

Related Questions