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
|
else if(command == 'f'){
char read;
std::string collector; //filename
std::string sizellector; //filename
do{ //get filename
recv(sock_CONNECTION,&read,1,0); // for [filename]&
if(read !='&') //& is delimiter so no need to add
collector += read;
} while(read != '&'); //end at &
read =0;//empty
do{ //for [1400000]& which is the size
recv(sock_CONNECTION,&read,1,0); //get file type
if(read !='&') //& is delimiter so no need to add
sizellector += read; //
}while(read != '&'); //end
//collect and complete
long size = atol(sizellector.c_str()); //convert string to int
char * buffer = new char[size]; //alocate
char ptr[1024]; //buffer
int var = 0;
while(var <= size){ //
recv(sock_CONNECTION,ptr,1024,0); //receive
strcat(buffer,ptr); //concatenate
var=var+1024; //increase
}
std::ofstream outfile (collector,std::ofstream::binary);
outfile.write (buffer,size); //write
SetConsoleTextAttribute(hConsole, 2);
std::cout<<" >Client:has sent a file named "<<collector<<" size is "<<sizellector<<std::endl; //output
SetConsoleTextAttribute(hConsole, csbiInfo.wAttributes);
send(sock_CONNECTION, "File transfer is successful",28 ,NULL);
delete [] buffer
outfile.close();
}
|