Einzelnen Beitrag anzeigen

Briand
(Gast)

n/a Beiträge
 
#10

AW: TCP Filelistening

  Alt 24. Jan 2014, 12:40
Also ich hab das nun so gelöst weiss zwar nicht ob es der richtige Weg ist aber es funktioniert

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Winapi.Messages, SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Buttons, Web.Win.Sockets,
  System.Win.ScktComp, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.ExtCtrls;
type
  TForm1 = class(TForm)
    SpeedButton1: TSpeedButton;
    client: TClientSocket;
    server: TServerSocket;
    SpeedButton2: TSpeedButton;
    lv1: TListView;
    Edit1: TEdit;
    Procedure SpeedButton1Click(Sender: TObject);
    Procedure clientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
    Procedure serverClientRead(Sender: TObject; Socket: TCustomWinSocket);
    Procedure clientRead(Sender: TObject; Socket: TCustomWinSocket);
    Punction ListFiles(sDir: String): String;
    Punction GetFileSize(sFile: PChar): Int64;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }

  end;

var
  Form1: TForm1;
  LI2: TListItem;
  ListedFiles: Ansistring;
  TotalFiles: integer;
  i : integer;
implementation

Uses Unit3;
  {$R *.dfm}

Function ListFiles(sDir: String): String;
Var
  sFileName: String;
  sFileList: String;
  sSizeList: String;
  sRec: TWin32FindData;
  findHandle: THandle;
  Begin
  totalfiles := 0;
 If AnsiLastChar(sDir) <> '\Then
 Begin
   sDir := sDir + '\';
 End;
  Try
    findHandle := FindFirstFile(PChar(sDir + '*.*'), sRec);
    If findHandle <> INVALID_HANDLE_VALUE Then
      Repeat
        sFileName := sRec.cFileName;
        If (sRec.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) <> 0 Then Begin
        End
        Else
        Begin
        sFileList := sFileList + sFileName +'¦'+ IntToStr(GetFileSize(PChar(sDir + sFileName))) + '|';
        totalfiles := totalfiles + 1;
        End;
      Until FindNextFile(findHandle, sRec) = False;
  Finally
  End;
  Result := sFileList;
End;

Function GetFileSize(sFile: PChar): Int64;
Var
  fFile: THandle;
  wFD: TWIN32FINDDATA;
Begin
  Result := 0;
  If Not FileExists(sFile) Then
  Begin
  Exit;
  End;
  fFile := FindFirstFile(PChar(sFile), wFD);
  If fFile = INVALID_HANDLE_VALUE Then
  Begin
  Exit;
  End;
  Result := (wFD.nFileSizeHigh * (Int64(MAXDWORD) + 1)) + wFD.nFileSizeLow;
  Windows.FindClose(fFile);
End;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
form1.server.Socket.Connections[0].SendText('FileStart|');
form1.lv1.Clear;
end;

procedure TForm1.clientError(Sender: TObject; Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
errorcode := 0;
end;

procedure TForm1.clientRead(Sender: TObject; Socket: TCustomWinSocket);
var cData, Ccmd: string;

begin
 cData := socket.ReceiveText;
 Ccmd := copy(cData,0,pos('|',cData)-1);

 if cCmd = ('FileStart') then
 begin
 ListedFiles := ListFiles('C:\MeineFotos\ordner1');
 form1.Caption := inttostr(totalfiles);
if totalfiles = 0 then
begin
  exit
end;
 form1.client.Socket.SendText('FileADD|'+ copy(ListedFiles,0,pos('|',ListedFiles)-1));
 Delete(ListedFiles,1,pos('|',ListedFiles));
 totalfiles := totalfiles -1;
 end;

 if cCmd = ('FileNEXT') then
 begin
 if totalfiles = 0 then
 begin
 exit
 end;

 if Totalfiles = 1 then
 begin
 form1.client.Socket.SendText('FileEND|'+ copy(ListedFiles,0,pos('|',ListedFiles)-1));
 end
 else begin
 form1.client.Socket.SendText('FileADD|'+ copy(ListedFiles,0,pos('|',ListedFiles)-1));
 Delete(ListedFiles,1,pos('|',ListedFiles));
 totalfiles := totalfiles -1;
 end;
 end;

end;

procedure TForm1.serverClientRead(Sender: TObject; Socket: TCustomWinSocket);
var
  sData, sCmd: string;

begin
 sData := Socket.ReceiveText;
 sCmd := copy(SData,0,pos('|',sData)-1);

 if sCmd = ('FileADD') then
 begin
 Delete(sdata,1,pos('|',sdata));
 LI2 := form1.lv1.Items.Add;
 LI2.Caption := copy(sdata,0,pos('¦',sdata)-1);
 Delete(sdata,1,pos('¦',sdata));
 LI2.SubItems.Add(sdata);
 form1.server.Socket.Connections[0].SendText('FileNEXT|');
 end;

 if sCmd = ('FileEND') then
 begin
 Delete(sdata,1,pos('|',sdata));
 LI2 := form1.lv1.Items.Add;
 LI2.Caption := copy(sdata,0,pos('¦',sdata)-1);
 Delete(sdata,1,pos('¦',sdata));
 LI2.SubItems.Add(sdata);

 end;

end.

Geändert von Briand (24. Jan 2014 um 12:43 Uhr)
  Mit Zitat antworten Zitat