Einzelnen Beitrag anzeigen

paresy

Registriert seit: 24. Aug 2004
Ort: Lübeck
105 Beiträge
 
Delphi 2007 Professional
 
#4

Re: Streams versenden (Ohne Indy's)

  Alt 20. Dez 2004, 17:14
senden:

Delphi-Quellcode:
uses Classes, ScktComp;

const BufferSize = 8192;

type
 TTSendStream = class(TThread)
  private
   FSocket: TCustomWinSocket;
   FStream: TStream;
   FRunning: Boolean;
  public
   constructor Create(CreateSuspended: Boolean; Socket: TCustomWinSocket; SourceStream: TStream);
   property Running : Boolean read FRunning;
  protected
   procedure Execute; override;
 end;

implementation


//------------------------------------------------------------------------------
// TTSendStream
//------------------------------------------------------------------------------
constructor TTSendStream.Create(CreateSuspended: Boolean; Socket: TCustomWinSocket; SourceStream: TStream);
begin

 FRunning := True;
 FSocket := Socket;
 FStream := SourceStream;

 inherited Create(CreateSuspended);

end;

//------------------------------------------------------------------------------
procedure TTSendStream.Execute;
var chunks, i, BytesRead, BytesWritten: Integer;
    Bfr: Pointer;
begin

 GetMem(Bfr, BufferSize);
 chunks := Ceil(FStream.Size / BufferSize);
 for i:=0 to chunks-1 do
  begin
   BytesRead := FStream.Read(Bfr^ , BufferSize);
   BytesWritten := FSocket.SendBuf(Bfr^, BytesRead);
   //Wenn Winsock Buffer voll, retry...
   while BytesRead <> BytesWritten do
    begin
     sleep(100);
     BytesWritten := FSocket.SendBuf(Bfr^, BytesRead);
    end;
  end;
 FreeMem(Bfr);

 FRunning := False;

//------------------------------------------------------------------------------
//--- USAGE
//------------------------------------------------------------------------------

 Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);

 Thread := TTSendStream.Create(False, Socket, Stream);
 while Thread.Running do
  begin
   Application.ProcessMessages;
   sleep(10);
  end;
 Thread.Free;
  
 Stream.Free;


end;
und hier empfangen

Delphi-Quellcode:
   while FormPos < FormSize do
    if fConnectionFormData.Socket.ReceiveLength > 0 then
     begin
      Len := fConnectionFormData.Socket.ReceiveLength;
      GetMem(Data, Len);
      ReadSize := fConnectionFormData.Socket.ReceiveBuf(Data^, Len);
      Inc(FormPos, ReadSize);
      Stream.WriteBuffer(Data^, ReadSize);
      FreeMem(Data);
      Event(FormTag, FormPos, FormSize);
     end
    else
     sleep(100);
sind code snippets aus den projekt... also musst du für deine bedürnisse evtl noch anpassen.... aber zumindest von grundprinzip...

grüße paresy
  Mit Zitat antworten Zitat