1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
void download(SOCKET &server, string filename, const int BUFF = 100000) {
cout << "file: " << filename << "\n";
FILE *readfp;
size_t filesize;
size_t numread, tmpnumread;
size_t iters;
int next = 1;
unsigned exit_flag = false, exit_flag_server = false;
if ((readfp = fopen64(filename.c_str(), "rb")) == nullptr) {
exit_flag = true;
send(server, (char *) &exit_flag, sizeof(exit_flag), 0);
return; // error while opening
}
send(server, (char *) &exit_flag, sizeof(exit_flag), 0); // all right (flag == false)
if (recv(server, (char *) &exit_flag_server, sizeof(exit_flag_server), 0); exit_flag_server) {
cerr << "server error recognized\n";
fclose(readfp);
return;
}
// get file size
_fseeki64(readfp, 0, SEEK_END);
filesize = _ftelli64(readfp);
_fseeki64(readfp, 0, SEEK_SET);
iters = (filesize % BUFF == 0 ? (filesize / BUFF) : ((filesize / BUFF) + 1));
cout << "iters: " << iters << "\n";
//send(server, (const char *) &iters, sizeof(iters), 0);
char *buffer = (char *) malloc(BUFF);
while ((numread = fread(buffer, sizeof(char), BUFF, readfp)) > 0) {
send(server, (char *) &numread, sizeof(numread), 0);
recv(server, (char *) &numread, sizeof(numread), 0);
send(server, buffer, numread * sizeof(char), 0);
recv(server, (char *) &numread, sizeof(numread), 0);
}
fclose(readfp);
free(buffer);
}
|