Einzelnen Beitrag anzeigen

T3rm1n4T0r

Registriert seit: 10. Sep 2004
38 Beiträge
 
Turbo Delphi für Win32
 
#7

Re: Dateien mit Client/Serversocket versenden. KEIN INDY!

  Alt 12. Sep 2004, 13:52
Hab jetzt nochmal was verändert. Mein Problem jetzt ist, dass der Dateiname nicht NUR den Dateinamen enthält, sondern den ganzen Rest auch. Ich verstehe aber nicht warum. In dem Beispiel passiert das ja auch nicht?

Delphi-Quellcode:
procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
var EmpfTxt : string;
    nReceived : Longint;
    datei : TFileStream;
begin
                          case CliStatus of
                          csIdle: begin
                                    Socket.ReceiveBuf(Buffer, sizeof(Buffer));
                                    if (copy(Buffer,1,5) = 'Datei') then CliStatus := csFile;
                                    if (copy(Buffer,1,4) = 'Text')
                                    then begin
                                      EmpfTxt := Buffer;
                                      delete(EmpfTxt,1,4);
                                      EChatWindow.SelAttributes.Color := clblue;
                                      EChatWindow.Lines.Add(EmpfTxt);
                                    end;
                                    if (copy(Buffer,1,5) = 'DName')
                                    then begin
                                      EmpfTxt := Buffer;
                                      delete(EmpfTxt,1,5);
                                      Dateiname := EmpfTxt;
                                    end;
                                    Buffer := '';
                                  end;
                          //ankommende Datei
                          csFile: begin
                                    if not Working then
                                    begin
                                      working := true;
                                      datei := TFileStream.Create(ExtractFilePath(Application.ExeName)+'\'+dateiname, fmCreate);
                                      try
                                        while True do
                                        begin
                                          nReceived := Socket.ReceiveBuf(Buffer, sizeof(Buffer));
                                          if nReceived <= 0 then Break
                                          else Datei.WriteBuffer(Buffer, nReceived);
                                          Sleep(200);
                                        end;
                                        Datei.position := 0;
                                      finally
                                        Datei.Free;
                                      end;
                                      CliStatus := csIdle;
                                      working := false;
                                    end;
                                  end;
                          end;
end;

procedure TForm1.btnsendfilesClick(Sender: TObject);
var i, k : Longint;

begin
  for i := 0 to ListBox1.Items.Count-1 do
  begin

    dateiname := ListBox1.Items[i];
    k := length(dateiname);
    while (dateiname[k] <> '\') do dec(k); //Dateiname rausfinden
    dateiname := copy(dateiname,k+1,length(dateiname)-k);
    try
      if server
      then begin
        ServerSocket1.Socket.Connections[0].SendText('DName' + Dateiname); //dateiname senden
        ServerSocket1.Socket.Connections[0].SendText('Datei');
        ServerSocket1.Socket.Connections[0].SendStream(TFileStream.Create(Listbox1.Items[i], fmOpenRead or fmShareDenyWrite));
      end
      else begin
        ClientSocket1.Socket.SendText('DName' + Dateiname);
        ClientSocket1.Socket.SendText('Datei'); //sagen dass jetzt ne Datei kommt
        ClientSocket1.Socket.SendStream(TFileStream.Create(Listbox1.Items[i], fmOpenRead or fmShareDenyWrite));
      end;
    finally
 // NICHTS
    end;
  end;
end;
  Mit Zitat antworten Zitat