码迷,mamicode.com
首页 > Windows程序 > 详细

WinSock实现的大文件传输

时间:2014-05-18 08:58:26      阅读:479      评论:0      收藏:0      [点我收藏+]

标签:socket   文件传输   broadcast   

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 :"<<flen<<endl; rewind(pFile); partsnum=flen/BUF_LEN+1; send(send_s,(char *)(&partsnum),sizeof(partsnum),0); cout<<"File parts num:"<<partsnum<<endl; cout<<"File name:"<<filename<<endl; str=new char[BUF_LEN]; int i; if(partsnum>1) { plen=BUF_LEN; } for(i=0;i<partsnum;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; cout<<percentage<<"\%complete!"<<endl; } //cout<<"File parts send:"<<i<<endl; //cout<<"File parts length is:"<<plen<<endl; //cout<<"Read content is:"<<str<<endl; } fclose(pFile); } private: int flen,plen,partsnum,percentage; char * str; };


接收端:

class file_rev { public: SOCKET rev_s; char * filename; void rev_file() { FILE * fn=fopen(filename,"wb"); recv(rev_s,(char *)(&partsnum),sizeof(partsnum),0); cout<<"File parts num:"<<partsnum<<endl; int i; for(i=0;i<partsnum;i++) { recv(rev_s,(char *)(&flen),sizeof(flen),0); buf=new char[flen]; recv(rev_s,buf,flen,0); //cout<<"File parts received:"<<i<<endl; //cout<<"File parts length:"<<flen<<endl; fwrite(buf,1,flen,fn); } fclose(fn); } private: int flen,plen,partsnum; char * buf; };

WinSock实现的大文件传输,布布扣,bubuko.com

WinSock实现的大文件传输

标签:socket   文件传输   broadcast   

原文地址:http://blog.csdn.net/luguifang2011/article/details/26072077

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!