|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
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,请问问题出在那里
|
|