文件传输的问题,帮忙看一下(100分)

  • 主题发起人 主题发起人 1.0
  • 开始时间 开始时间
1

1.0

Unregistered / Unconfirmed
GUEST, unregistred user!
会发生数据接收不全,这是怎么回事,麻烦请指出错在哪

服务端

procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
C : PCON;
cmd:String;
Buffer : pointer;
nRetr : integer;
ReadCount : integer;
const
bufferSize = 1024 ;
begin
C:= Socket.Data ;
case c.Status of
0 :
begin
cmd := trim(Socket.ReceiveText) ;

if Pos('UPLOAD ',uppercase(cmd)) > 0 then
begin
c.FileName := trim(Copy(cmd,Pos(' ',cmd)+1,Length(cmd)));
c.TotalSize := StrToInt(Copy(c.FileName,Pos(' ',c.FileName)+1,Length(c.FileName)));
c.FileName := trim(Copy(c.FileName,1,Pos(' ',c.FileName)));
c.Status := 1;
Socket.Data := C;
fs :=TFileStream.Create('data/'+c.FileName,fmCreate or fmShareDenyNone);
fs.Free;
// fs.Seek(0,soFromBeginning);
Socket.SendText('SEND');
end;
end;
1 : begin
inc(i);
GetMem(Buffer,BufferSize);
nRetr := Socket.ReceiveBuf(Buffer^,BufferSize);
if FIleExists('data/'+c.FileName) then
begin
fs :=TFileStream.Create('data/'+c.FileName,fmOpenWrite or fmShareDenyNone);
fs.Seek(0,soFromEnd);
end;

fs.WriteBuffer(Buffer^,nRetr);
// fs.Destroy;
FreeMem(Buffer);
end;
end;

end;


客户端
procedure TForm2.CSRead(Sender: TObject; Socket: TCustomWinSocket);
var
rlt : String;
fs : TFileStream;
Buf : pointer;
nRead : Integer;
m_bSending : boolean;
lBytesSent : longint;
i:integer;
begin
rlt := Socket.ReceiveText;
i:=0;
if rlt='SEND' then
begin
lBytesSent:=0;
m_bSending := TRUE;
fs := TFileStream.Create(c.FileName,fmOpenRead or fmShareDenyNone);
GetMem(Buf,fs.Size);
while m_bSending do
begin
inc(i);
if (fs.Seek(lBytesSent,soFromBeginning) = lBytesSent) then
begin
nRead := fs.Read(Buf^,1024);
try
Form2.ProgressBar1.Position := fs.Position;
Cs.Socket.SendBuf(Buf^,nRead);
except
end;
if nRead < 1024 then
begin
lBytesSent := lBytesSent + nRead;
m_bSending := false;
end
else
begin
// Sleep(2);
lBytesSent := lBytesSent + 1024;
end;
end
else
m_bSending := false;
end;//endwhile
end;//endif
fs.Destroy;
FreeMem(Buf);
showMessage(inttostr(i));
end;
 
服务器端要判断接受的数据加起来是否等于发送文件的尺寸,如果是就退出
 
你怎么又贴到这里来了,
这两个组件丢包是正常现象,如果自己不定校验机制
 
to jiajiajia888
如果sleep(20)的话,这样还会发生丢包吗?
 
在csread里收到返回-1为止就不丢数据了。
 
按我上面的程序,返回不到-1吧
 
你可以看看delphi里sendbuf的说明。
 
后退
顶部