鱼C论坛

 找回密码
 立即注册
查看: 2994|回复: 1

socket传输文件问题

[复制链接]
发表于 2012-12-31 12:54:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
int nChunkCount=0;
        FILE *fp=fopen(findfile2,"w");
        if(NULL == fp )
        {
          cout<<"File:\ts Not Found\n";
        }
        else
        {       
                        fseek(fp,0L,SEEK_SET);
                        recv(*clienth,(char*)&cmd,sizeof(cmd),0);
                        int Filelen=cmd.ID;
                        cout<<"文件大小:"<<Filelen;
                        nChunkCount=Filelen/512;
                        if((Filelen%512)!=0)
                        {nChunkCount++;}
                        char* date=new char[512];
                        cout<<"nChunkCount="<<nChunkCount<<endl;
                        for(int i=0;i<nChunkCount;i++)
                                {
                                        int nLeft;
                                        if(i+1==nChunkCount)
                                          {nLeft=Filelen-512*(nChunkCount-1);}
                                        else
                                          {nLeft=512;}
                                        int idx=0;
                                       
                                        while(nLeft>0)
                                         {
                                          // int ret=send(*clienth,&date[idx],nLeft,0);
                                           int ret=recv(*clienth,&date[idx],nLeft,0);
                                           if(ret==SOCKET_ERROR)
                                           {
                                            cout<<"文件传输过程中发生错误"<<endl;
                                           }
                                           else
                                           {
                                           nLeft-=ret;
                                           idx+=ret;
                                           }
                                         }
                                                        cout<<date;
                                        fwrite(date,sizeof(char),idx,fp);
                                }
                                                                     //这段代码是循环读取文件的一段数据,在循环调用send,发送到客户端,这里强调一点的TCP每次接受最多是1024字节,多了就会分片,因此每次发送时尽量不要超过1024字节。
//            close(fp);
            
                        delete[]  date;
          cout<<"File: Transfer Finishedn";
        }
                fclose(fp);
以上为接收数据
        int nChunkCount=0;
        FILE *fp=fopen(findfile2,"r");
        if(NULL == fp )
        {
          cout<<"File:\ts Not Found\n";
        }
        else
        {       
            int file_block_length = 0;
                        int Filelen=0;
                        fseek(fp,0,SEEK_END);
                        Filelen=ftell(fp);
                        fseek(fp,0,SEEK_SET);
                       
                        memcpy((char*)cmd.lparam,(char*)findfile2,sizeof(findfile2));
                        cmd.ID=4;
                        send(*clienth,(char*)&cmd,sizeof(cmd),0);
                        cmd.ID=Filelen;
                        send(*clienth,(char*)&cmd,sizeof(cmd),0);
                        cout<<"文件大小:"<<cmd.ID;
                        nChunkCount=Filelen/512;

                        if((Filelen%512)!=0)
                        {nChunkCount++;}
                        char* date=new char[512];
                        cout<<"nChunkCount="<<nChunkCount<<endl;
                        for(int i=0;i<nChunkCount;i++)
                                {
                                        int nLeft;
                                        if(i+1==nChunkCount)
                                          {nLeft=Filelen-512*(nChunkCount-1);}
                                        else
                                          {nLeft=512;}
                                        int idx=0;
                                        fread(date,sizeof(char),nLeft,fp);
                                        cout<<date;
                                        while(nLeft>0)
                                         {
                                           int ret=send(*clienth,&date[idx],nLeft,0);
                                           if(ret==SOCKET_ERROR)
                                           {
                                            cout<<"文件传输过程中发生错误"<<endl;
                                           }
                                           else
                                           {
                                           nLeft-=ret;
                                           idx+=ret;
                                           }
                                         }
                                }
                                                                     //这段代码是循环读取文件的一段数据,在循环调用send,发送到客户端,这里强调一点的TCP每次接受最多是1024字节,多了就会分片,因此每次发送时尽量不要超过1024字节。
//            close(fp);
           
                        delete[]  date;
          cout<<"File: Transfer Finishedn";
        }
                 fclose(fp);
以上为发送数据
接收几KB的文件时正确,但是上M文件时,接收后的文件要多几KB,请问问题出在那里
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2013-1-28 15:40:54 | 显示全部楼层
本帖最后由 FJK 于 2013-1-28 15:42 编辑

看来也是看的 远程控制教程啊

m应该没问题,出问题的大于多少G的,主要是因为获取的文件大小超过限制 你确定你是这个问题?
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-8-9 20:22

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表