Einzelnen Beitrag anzeigen

Benutzerbild von Harry M.
Harry M.

Registriert seit: 29. Okt 2004
Ort: Halle
462 Beiträge
 
#1

(Indy) TCPClient bekommt zuviel vom Server

  Alt 20. Apr 2006, 18:08
Mahlzeit!

Ich möchte mit einem Indy TCPClient von einem Indy TCPServer eine Datei holen (was auch sonst ) Der Server sendet dabei (im Debugg-Modus) einen 2048 Byte grossen Stream an den Client dieser empfängt aber gleich mehr als die ganz Datei

Der Client;
Delphi-Quellcode:
procedure TDownloadThread.DownloadFile;
begin
  TCPClient := TIdTCPClient.Create(nil);
  TCPClient.Host := sHost;
  TCPClient.Port := iPort;

  try
    TCPClient.Connect(-1);
    except end;

  if TCPClient.Connected then begin
    TCPClient.WriteLn('GETSIZE='+sFileName);

    iRemoteFileSize := StrToInt(TCPClient.ReadLn);

    if iRemoteFileSize > 0 then begin
      TCPClient.WriteLn('GETFILE='+sFileName);

      fs := TFileStream.Create(sFileName + '.copy' ,fmCreate);
      ms := TMemoryStream.Create;

      sStatus := StatusWork;
      sProgress := IntToStr( Round(fs.Size/iRemoteFileSize*100) ) + '%';
      Synchronize(SyncProgress);

      while (TCPClient.Connected) and (fs.Size < iRemoteFileSize) do begin
       ms.Clear;

       try
        TCPClient.ReadStream(ms); // hier sollte er immer nur 2048 bzw den Rest bekommen, tut er aber nicht :-(
        except end;

        if ms.Size > 0 then
          fs.CopyFrom(ms, 0);

        sProgress := IntToStr( Round(fs.Size/iRemoteFileSize*100) ) + '%';
        Synchronize(SyncProgress);
        end;

      FreeAndNil(fs);
      FreeAndNil(ms);
      end;

    end;

  sStatus := StatusDone;
  Synchronize(SyncProgress);
  TCPClient.Free;
end;
Der Server, welcher meiner Meinug nach läuft:
Delphi-Quellcode:
procedure TVPumper.IdTCPServer1Execute(AThread: TIdPeerThread);
var
  fs: TFileStream;
  ms: TMemoryStream;
  I: Integer;
  iBytes2Send: Cardinal;
  S, sCommand, sFileName, sHash: String;
  slFileList: TStringList;
begin
  S := UpperCase(AThread.Connection.ReadLn);

  slFileList := TStringList.Create;

  sCommand := Copy(S, 1, 7);
  sFileName := Copy(S, 9, Length(S));

  // Dateiliste erstellen
  if sCommand = 'GETLISTthen begin
    if CreateFileList(ShreadFolder + '*.*', sFileName, slFileList) then
      slFileList.Text := StringReplace(slFileList.Text, '', '', [rfReplaceAll]);
      AThread.Connection.WriteLn(slFileList.Text);
      AThread.Connection.Disconnect;
    end;
                       
  if sCommand = 'GETSIZEthen begin

    sHash := Copy(S, 9, Length(S));

    if CreateFileList(ShreadFolder + '*.*', sHash, slFileList) then begin
      I := Pos('|', slFileList.Text) + 1;
      slFileList.Text := Copy(slFileList.Text, I, Length(slFileList.Text));

      I := Pos('|', slFileList.Text) - 1;
      slFileList.Text := Copy(slFileList.Text, 1, I);

      AThread.Connection.WriteLn(slFileList.Text);
      //AThread.Connection.Disconnect;
      end;

    end;

  if sCommand = 'GETFILEthen begin
    sHash := Copy(S, 9, Length(S));
    sFileName := Hash2File(sHash);

    fs := TFileStream.Create(sFileName ,fmOpenRead   + fmShareDenyNone);
    ms := TMemoryStream.Create;

    while fs.Position < fs.Size do begin
      ms.Clear;

      iBytes2Send := fs.Size - fs.Position;

      if iBytes2Send > 2048 then
        iBytes2Send := 2048;

      ms.CopyFrom(fs, iBytes2Send);

      AThread.Connection.OpenWriteBuffer;
      AThread.Connection.WriteStream(ms, True, True); // immer fleißig 2048 schauffeln, bzw den Rest
      AThread.Connection.CloseWriteBuffer;

      end;

    AThread.Connection.Disconnect;
    FreeAndNil(fs);
    FreeAndNil(ms);
    end;

  slFileList.Free;
end;
Wo liegt der Fehler (ausser im Detail ) ?
Harry
Gruß Harry
www.H-Soft.info
  Mit Zitat antworten Zitat