无法将从FTP客户端接收的图像传输到我的FTP服务器上

时间:2014-06-18 05:06:14

标签: c++ ftp

使用下面的代码,我能够很好地读取和存储FTP客户端发送到我的FTP服务器的文本文件,但图像和其他二进制数据最终被破坏。我确定我忽略了in the spec。错误是否明显?

    //open the file sepcified for output
    std::ofstream stor_file(cmd_arg, std::ios::binary);

    unsigned buf_size = 500;
    unsigned char *buf = new unsigned char[ buf_size ];

    const char *open_conn = "150 openeing data connection\r\n";
    CNetwork::write_to_socket( cntl_fd, open_conn, strlen(open_conn) );

    // slurp the whole file from the socket 
    int bytes_read = read( data_fd, buf, buf_size);

    while (bytes_read > 0)
    {
        // write what's in the buffer to the file
        for (int i = 0; i < bytes_read; ++i)
        {
            stor_file << buf[i];
        }

        memset( buf, 0, sizeof( buf ) );
        bytes_read = read( data_fd, buf, sizeof( buf ));
    }

    delete [] buf;

    // done writing...close the file!
    stor_file.close();

    // write to socket: "226 File successfully transferred\r\n";

0 个答案:

没有答案