c ++ socket二进制文件

时间:2012-01-04 16:37:07

标签: c++

我有这个函数来获取文件的内容,

#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;
    }

并将其发送到服务器

 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);
    }

这是接收函数

 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();
}
}

,结果是一个破管道(我用g ++编译器将其运行到freebsd 6.4 amd),所以我想念,连接都很好我可以将文本从客户端传输到服务器并且反向问题是二进制文件

1 个答案:

答案 0 :(得分:1)

我发现您的代码有两个问题:

  1. 您正在进行大量分配(new),但您永远不会释放内存。
  2. SendS函数中,您获取字符串长度,但该“字符串”中的数据来自整数向量,并且是二进制的。这意味着数据可以包含字符串终止'\0'字符(整数0)。
  3. 除此之外,我真的不遵循你正在做的事情。而不是读取向量,创建一个char-buffer并分配足够的内存将整个文件放入该缓冲区(char *buffer = new char[length_of_file])并发送它,首先是缓冲区的长度。

    这样的事情:

    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;
        }
    }
    

    在接收方做类似的事情。