[Boost-users] ASIO: What is the proper way to upload a LARGE file?

656 views
Skip to first unread message

Ramon F Herrera

unread,
Jan 13, 2011, 11:11:47 AM1/13/11
to boost...@lists.boost.org

I have successfully been using this code to upload small stuff:

try {
// open tcp connection
tcp::iostream net_stream(strSubmitServer, "12345");

// send file name
net_stream << strFile << endl;

// copy file
char ch;
while ((ch = fgetc(f)) != EOF)
net_stream << ch;

} catch (std::exception& e) {
std::cout << "Exception: " << e.what() << std::endl;
}

Needless to say, the code above is extremely slow/inefficient for bulky
transfers.

Now I need to upload some large fails, and tried this:

tcp::iostream net_stream(SERVER_ADDR, "12345");
// send file name
net_stream << IN_FILE << "\n";
// send tags data size
net_stream << count << "\n";
// copy tags data
net_stream.write((char *)data, count); <<== app crashes here

I figured that TCP would break the delivery into packets...

TIA,

-Ramon


_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Igor R

unread,
Jan 13, 2011, 4:34:01 PM1/13/11
to boost...@lists.boost.org
> Now I need to upload some large fails, and tried this:
>
>  tcp::iostream net_stream(SERVER_ADDR, "12345");
>  // send file name
>  net_stream <<  IN_FILE << "\n";
>  // send tags data size
>  net_stream <<  count << "\n";
>  // copy tags data
>  net_stream.write((char *)data, count);  <<== app crashes here

If it crashes, then most likely the "data" buffer is smaller than
count. What is "data", how do you allocate it?

michi7x7

unread,
Jan 13, 2011, 5:04:05 PM1/13/11
to boost...@lists.boost.org
Hi,

why don't you just do something like this?


tcp::iostream net_stream(SERVER_ADDR, "12345");

std::ifstream fstream("file");

net_stream << fstream.rdbuf();


It's up to the stream-implementations to buffer the data appropriately then.

Regards,

michi7x7

Reply all
Reply to author
Forward
0 new messages