Einzelnen Beitrag anzeigen

Benutzerbild von cherry
cherry

Registriert seit: 14. Nov 2005
561 Beiträge
 
RAD-Studio 2009 Ent
 
#23

Re: indy10 / TCPServer /TCPClient ->Datei versenden

  Alt 9. Apr 2008, 06:13
Allen vielen Dank für eure Hilfe...

Fürs erste hab ichs nun hinbekommen, scheint jedenfalls zu funktionieren. Eine kleine Frage hätte ich da aber noch, wie kann man nun die Progressbar des "uploaders" (TCPServer) anzeigen lassen?

hier mein aktueller code:

Delphi-Quellcode:
// SEND FILE / ACCEPT INCOMMING FILE (same button)
procedure TForm1.EButton2Click(Sender: TObject);
begin
  if EButton2.Caption = 'uploadthen
  begin
    if OpenDialog1.Execute then
    begin
      try
        uploadfilepath := OpenDialog1.FileName;
        EButton2.Enabled := False;
        EButton5.Enabled := False;
        TCPClient.Connect;
        // upload file
        TCPSendCmd(CmdUploadFile);
        TCPClient.IOHandler.WriteLn(ExtractName(uploadfilepath));
        Log(msg001);
        Log('wait for client reply...');
      except
        on e:Exception do
        begin
          Log(e.Message);
          EButton2.Enabled := True;
        end;
      end;
    end;
  end
  else
  begin
    try
      ButtonCaption('receive');
      EButton2.Enabled := False;
      EButton4.Enabled := False;
      EButton5.Enabled := False;
      TCPClient.Connect;
      // accept file
      TCPSendCmd(CmdRecFileOK);
    except
      on e:Exception do
        Log(e.Message);
    end;
  end;
end;
und

Delphi-Quellcode:
procedure TForm1.TCPServerExecute(AContext: TIdContext);
var
  FSTream: TFileStream;
  df: String;
  tmpClientData : TClientData;
begin

  if not assigned(AContext.Data) then
  begin
    AContext.Connection.Disconnect;
    exit;
  end;

  // read clientdata
  try
    tmpClientData := TClientData(AContext.Data);
  except
    AContext.Connection.Disconnect;
    exit;
  end;

  // check state
  if (tmpClientData.Status = cCSGoing2Disconnect) or
     (tmpClientData.Status = cCSDisconnected) then
  begin
    AContext.Connection.Disconnect;
    exit;
  end;

  // get command
  try
    tmpClientData.LastCmd := AContext.Connection.Socket.ReadLn(#$A,5000,1024);
  except
    tmpClientData.LastCmd := '';
  end;

  // check the old command
  case tmpClientData.LastIntCmd of
    // GUY WANTS TO SEND FILE
    1:
    begin
      savefilepath := DestinationEdit.Text + AContext.Connection.IOHandler.ReadLn;
      Log(AContext.Binding.PeerIP+' wants to send file: "'+ExtractName(savefilepath)+'"');
      ButtonCaption('receive');
      EButton4.Enabled := True;
    end;
    // UPLOAD FILE
    2:
    begin
      Log('upload file...');
      TCPSendCmd(4);
      try
        // upload
        FStream := TFileStream.Create(uploadfilepath, fmOpenRead or fmShareDenyWrite);
        TCPClient.IOHandler.Write(FStream,0,true);
        // disconnect
        AContext.Connection.Disconnect;
        if TCPClient.Connected then
          TCPClient.Disconnect;
        tmpClientData.Status := cCSDisconnected;
      finally
        FStream.Free;
      end;
      Log('successfully uploaded');
      ButtonCaption('upload');
      EButton4.Enabled := False;
      EButton2.Enabled := True;
      EButton5.Enabled := True;
    end;
    // GUY REFUSED FILE
    3:
    begin
      Log('guy refused to download file');
      // disconnect
      AContext.Connection.Disconnect;
      if TCPClient.Connected then
        TCPClient.Disconnect;
      tmpClientData.Status := cCSDisconnected;
      ButtonCaption('upload');
      EButton4.Enabled := False;
      EButton2.Enabled := True;
      EButton5.Enabled := True;
    end;
    // DOWNLOAD FILE
    4:
    begin
      try
        if FileExists(savefilepath) then
        begin
          Log('file was already existing');
          df := savefilepath;
          DeleteFile(df);
          Log('old file deleted!');
        end
        else if DirectoryExists(ExtractFilePath(savefilepath)) = false then
        begin
          CreateDir(ExtractFilePath(savefilepath));
          Log('directory "'+ExtractFilePath(savefilepath)+'" created');
        end;
        Log('download...');
        try
          // download
          FStream := TFileStream.Create(savefilepath, fmCreate);
          AContext.connection.IOHandler.ReadStream(fstream);
          // disconnect
          AContext.Connection.Disconnect;
          if TCPClient.Connected then
            TCPClient.Disconnect;
          tmpClientData.Status := cCSDisconnected;
        finally
          FStream.Free;
        end;
        Log('file successfully downloaded!');
        ButtonCaption('upload');
        EButton4.Enabled := False;
        EButton2.Enabled := True;
        EButton5.Enabled := True;
      except
        on e:Exception do
          Log(e.Message);
      end;
    end;
    // RECEIVE CHAT MESSAGE
    5:
    begin
      Log(AContext.Binding.PeerIP+': '+AContext.Connection.IOHandler.ReadLn);
      AContext.Connection.Disconnect;
    end;
  end;
end;
hehe

Delphi-Quellcode:
procedure TForm1.TCPSendCmd(Cmd: Byte);
begin
  try
    TCPClient.IOHandler.WriteLn(IntToStr(Cmd));
  except
    on e:Exception do
      Log(e.Message);
  end;
end;
Ist das nur mein Gefühl, oder ist die ganze Welt verrückt geworden!?
  Mit Zitat antworten Zitat