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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
const char *dir = "/home/Desktop/text_files";
if (chdir(dir) == -1) {
perror(dir);
return 1;
}
DIR *dirp = opendir(".");
for (struct dirent *dent; (dent = readdir(dirp)) != NULL; )
{
const char *nm = dent->d_name;
if (strcmp(nm, ".") == 0 || strcmp(nm, "..") == 0)
continue;
struct stat file_stats;
if (stat(nm, &file_stats) == -1)
perror(nm);
/*else
{//printf("%9u: %s\n", (unsigned)file_stats.st_size, nm);
long converted_number = htonl((unsigned)file_stats.st_size);
int size_chunks=0;
size_chunks=send(new_socket, &converted_number, sizeof(converted_number), 0); //send image sizes
cout<<size_chunks<<"=====1=1=1=1========"<<"bytes"<<endl;
//break;
}*/
ifstream stream(nm, std::ios::in | std::ios::binary);
if(stream.is_open()) //open folder
{
vector<char> imageDataVec((istreambuf_iterator<char>(stream)), istreambuf_iterator<char>());
cout << "Size=of=image=== " << imageDataVec.size() << " bytes";
long conv_num= htonl(imageDataVec.size());
//send(new_socket, &converted_number, sizeof(converted_number), 0);
//send(new_socket, &imageDataVec, imageDataVec.size() , 0);
//size_t sent{};
int nbytes=0;
while (1)
{
//send(new_socket, &conv_num, sizeof(conv_num), 0);
nbytes = send(new_socket, &imageDataVec, imageDataVec.size(), 0);
//continue;
if (nbytes <= 0) {
std::clog << "error: while sending image\n";
break;
}
else
{
//sent += nbytes;
cout<<nbytes<<"=====1=1=1=1========"<<"bytes"<<endl;
}
break;
}
//fclose(fin);
}
else
{cout<<"can't open folder"<<endl;}
}
closedir(dirp);
close(s);
close(new_socket);
return 0;
}
| |