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;
服务端
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;