Board index » delphi » File transfer segment by segment error?

File transfer segment by segment error?

Hi;
I wrote an app to transfer file over internet/intranet. I want to display a
file transfer progress indicator but it is not possible with
'Socket.SendStream' that I know. So I am sending file with 'Socket.SendText'
command by 255 bayts segments. But most of transfer, some of the segments is
missing. Sometimes file is fully received but mostly it is short. There is
the code below that I used. If someone can help, I will be very pleased.
Best Regards...
Faruk

procedure TForm1.socClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin

end;

{************** Client Side ******************}
procedure TForm1.socClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  SendFileToServer('C:\Test.zip');
end;

procedure TForm1.SendFileToServer(LocalFileName: string);
const
  FileSegmentSize = 255;
var
  fs : TFileStream;
  c: integer;
  S: String;
begin
  c := FileSegmentSize;
  ProgressBar1.Position := 0;
  fs := TFileStream.Create(LocalFileName, fmOpenRead);
  try
    ProgressBar1.Max := fs.Size;
    SetLength(S, c);
    while fs.Position < fs.Size do
      begin
      if fs.Size - fs.Position < FileSegmentSize then
        begin
        c := fs.Size - fs.Position;
        SetLength(S, c);
        end;
      fs.Read(S[1], c);
      socClient.Socket.SendText(S);
      ProgressBar1.Position := fs.Position;
      Application.ProcessMessages;
      end;
  finally
    fs.Free;
  end;
end;

{************** Server Side ******************}
procedure TForm1.socServerClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  RemFile := TFileStream.Create(......);
end;

procedure TForm1.socServerClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
   RemFile.Free;
end;

procedure TForm1.socServerClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
var
  S: String;
begin
  S := Socket.ReceiveText;
  RemFile.Write(S[1], Length(S));
end;

 

Re:File transfer segment by segment error?


Quote
>    while fs.Position < fs.Size do
>      begin
>      if fs.Size - fs.Position < FileSegmentSize then
>        begin
>        c := fs.Size - fs.Position;
>        SetLength(S, c);
>        end;
>      fs.Read(S[1], c);
>      socClient.Socket.SendText(S);

You should check the result of SendText, and set the position
accordingly. SendText might not be able send everything, that's why it
returns the number of bytes it could actually pass on to winsock.

Other than that, the code looks ok.

Andy

Re:File transfer segment by segment error?


Millions and millions of thanks... I have been working on this problem for
two days.
Faruk
Quote

> You should check the result of SendText, and set the position
> accordingly. SendText might not be able send everything, that's why it
> returns the number of bytes it could actually pass on to winsock.

> Other than that, the code looks ok.

> Andy

Re:File transfer segment by segment error?


Millions and millions of thanks... I have been working on this problem for
two days.
Faruk
Quote

> You should check the result of SendText, and set the position
> accordingly. SendText might not be able send everything, that's why it
> returns the number of bytes it could actually pass on to winsock.

> Other than that, the code looks ok.

> Andy

Other Threads