DJJD
2010-07-28 17:18:25 UTC
Sending stream:
{Public}
FStream: TMemoryStream;
//(I have changed nothing here, the stream is perfect, but here's how I send it in the first place)
PrepareToSend; //Take Screen Shot to Memory Stream and load to FStream
//This works fine, I can load the stream into any other format
Socket.SendText(IntToStr(FStream.Size)+ #0 ); //send size of screenshot image (First send event)
//I don't know about the + #0 but it used to work just fine
FStream.Position:= 0; //Go to beginning of stream
try Socket.SendStream(FStream) except end; //send screenshot through a stream (Second send event)
Receiving stream:
{Public}
FData: TMemoryStream;
FDataSize: Integer;
FReciving: Bool;
(Private)
var Jpg: TJpegImage;
Len:string;
s: String;
s1,s2,s3: string;
p:integer;
Sz: Integer;
W: PWideChar;
Prepare to receive stream - after remote socket sends the size (First receive event)
FData:= TMemoryStream.Create; //Create memory stream 'S' comes in as '12915'#0 (size of stream)
SetLength(Len, StrLen(PChar(s))+1);
StrLCopy(@Len[1],PChar(s),Length(Len)-1);
FDataSize:= StrToIntDef(Len,0); //Set size of data
Delete(s,1,Length(Len));
FData.Write(s[1],length(s)); //Append size of data to stream
FReciving:= True; //next set of packets will be a stream
Receive each individual piece of the stream - after remote socket sends the stream (Second receive event)
FData.Write(s[1], length(s)); //Append new data to data stream
if (FData.Size = FDataSize) then begin //Check if all data has been received
FReciving:=false; //All data is received, no more is expected
try
Jpg := TJpegImage.Create; //Create JPEG Image object
FData.Position:= 0; //Go to beginning of data
Jpg.LoadFromStream(FData); //Fill JPEG with image data - THIS IS WHERE ERROR OCCURS
//(Removed extra irrelevant code to further process Jpg)
finally
Jpg.Free;
FData.Free;
end;
end;
So, somewhere in this process, the data in the stream must get messed up. Again, this used to work perfect in delphi 7, but not now in 2010. I think it has to do with converting the size in the first packet received.
If you can't figure out what's wrong with my code, can someone point me in a good direction where I can learn more about streams like this? This is the first time I've worked with memory streams.