WinSock实现的大文件传输
2020-12-12 22:23
class file_send { public: SOCKET send_s; //The socket that used for sending which is established previously char * filename; //The full path of the file that the client requested void send_file() { percentage=0; FILE * pFile=fopen(filename,"rb"); fseek(pFile,0,SEEK_END); flen=ftell(pFile); cout"File length is :"flenendl; rewind(pFile); partsnum=flen/BUF_LEN+1; send(send_s,(char *)(&partsnum),sizeof(partsnum),0); cout"File parts num:"partsnumendl; cout"File name:"filenameendl; str=new char[BUF_LEN]; int i; if(partsnum>1) { plen=BUF_LEN; } for(i=0;ipartsnum;i++) { if(i==(partsnum-1)) { plen=flen%BUF_LEN; } fread(str,1,BUF_LEN,pFile); send(send_s,(char *)(&plen),sizeof(plen),0); send(send_s,str,plen,0); if(percentage!=(i*100)/partsnum) { percentage=(i*100)/partsnum; coutpercentage"\%complete!"endl; } //cout //cout //cout } fclose(pFile); } private: int flen,plen,partsnum,percentage; char * str; };
下一篇:windows知识