Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems saving an incomming stream into a file

Status
Not open for further replies.

jamesp0tter

Programmer
Feb 20, 2003
119
PT
Hey guys,

like the title says, I can't save an incomming connection's data into a file, or at least not the correct data: I managed to TFileStream it but the data was all garbled, and seeing the file in an hex editor showed the contents of the saved data are very different from the actual one.

So, I ask, what can I do to resolve this?

I'm using the TSock component from Ward, might this be the problem?

I'm using an approach like this:
Code:
procedure TForm1.sock1Read(Sender: TObject; Count: Integer);
var fs:TFileStream;
begin
fs := TFileStream.Create('c:\temp\123.mp3',fmOpenReadWrite or fmCreate);
fs.Seek(0,soFromEnd);
(Sender as TSock).Stream.Read(fs,Count);
fs.Free;
end;
but it fires an exception

I tried a few other methods, like:
Code:
- var := (Sender as TSock).ReceiveCount(Count);
- fs.write(var,Count); // returns string of data
but also doesn't work, probably because saving a stream into a string isn't a very good idea (?).

If you guys have any ideas, I'd appreciate!

I preferably would like to keep the existing structure (firing the save-to-file stuff on the onRead event, which fires several times) rather than creating the socket and looping through incomming data, but if that's impossible to achieve I can try other methods :)

Thanks in advance!

jamesp0tter

p.s.: sorry for my (sometimes) bad english :p
 
For starters, I wouldn't be establishing the TFileStream each time your socket has read data. Set this up as a global variable or class field and set it up once when beginning the read. Technically this shouldn't make much difference to the data, but should increase read throughput.

Strings can handle 2GB of data, so if you're sure your streams won't be that much (i'd suggest anything up to 20-30MB would be ok, otherwise swap memory might start bogging everything down) then your second method would work fine.

You mention exceptions - but don't say what they are! These should be telling you exactly why and what the error is.
 
You're right, it does work, I just didn't notice a stupid error (I already had the contents of the stream before calling Receive(Count) again :S dumb..).

But I have another problem now, the data I save isn't ok! I made a test with a button that, when clicked, would save the contents of Edit4.Text (that is 'Edit4') to 'c:\temp\123.txt'. When I click the button, it saves data do the file, but when I open the file, it saved '̯œ 8' instead of 'Edit4' ..

What might be the problem ?

jamesp0tter

p.s.: sorry for my (sometimes) bad english :p
 
Post the relevent code here so we can see. It sounds to me as if one of your objects is not being Created.
 
I managed to solve it :)

My working code is:

Code:
  if FileExists(f) = true then
    begin
    fs := TFileStream.Create(f,fmOpenReadWrite or fmShareDenyWrite);
    fs.Seek(0,soFromEnd);
    end
    else
    fs := TFileStream.Create(f,fmCreate or fmShareDenyWrite);
    fs.Write(Pointer(s)^,Length(s));
    fs.Free;

Without Pointer(s)^ in the fs.Write line only garbled data would be saved, with Pointer(s)^ it now saves the correct data (text).

Thanks guys!

jamesp0tter

p.s.: sorry for my (sometimes) bad english :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top