zoukankan      html  css  js  c++  java
  • WinSock实现的大文件传输

    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; };

  • 相关阅读:
    !ps之前一次执行的内容
    shortcut switch in terminal start pos & end pos
    百度词典搜索_dress code
    修杰楷_百度百科
    红米让夏新没活路了,
    绝杀600元以下智能手机的夏新小V二代-专栏-速途网
    李宗瑞_百度百科
    监制_百度百科
    文件上传~Uploadify上传控件
    知方可补不足~CSS中的几个伪元素
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/6915633.html
Copyright © 2011-2022 走看看