Newsgroups : Borland : borland.public.delphi.internet.winsock : 2006 Aug : Re: Problem reading data from TidIOHandler

www.cryer.info
Managed Newsgroup Archive

Re: Problem reading data from TidIOHandler

Subject:Re: Problem reading data from TidIOHandler
Posted by:"Kiwi" (gl..@custommadesoftware.co.nz)
Date:Tue, 15 Aug 2006 15:41:55 +1200

I am still having problems handling the data from the IOHandler

This is the code on the Client Side.  The first 4 bytes that are recieved
indicate the length of the stream.
Here is the Server side code which seems to be working fine

    fMem.Position:= 0;
    Buf:= RawToBytes(fMem, fMem.Size);
    I:= Length(Buf); // = 73050
    SetLength(Buf, Length(Buf) +4);
    Move(Buf[0], Buf[4], Length(Buf));
    Move(I, Buf[0], 4); //90, 29, 1, 0
    AContext.Connection.IOHandler.Write(Buf);

Here is the Client side:
      fMem:= TMemoryStream.Create;
      try
        SetLength(Buf, 0);
        IOHandler.ReadBytes(Buf, 4, False);
        Move(Buf[0], Size, 4);
        if Size > 0 then
          IOHandler.ReadBytes(Buf, Size, False);
        fMem.Clear;
        BytesToRaw(Buf, fMem, Size);
        fMem.Position:= 0; <=====Failing here with an access violation
        fMem.Read(cmd, 4);
    finally
      fMem.Destroy;
  end;


"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:44e0b6d3$1@newsgroups.borland.com...
>
> "Kiwi" <glen@custommadesoftware.co.nz> wrote in message
> news:44dfe6d2@newsgroups.borland.com...
>
>>         IOHandler.ReadBytes(Buf, 1, False);
>
> Use ReadByte() instead:
>
>    Size := IOHandler.ReadByte;
>
>>         IOHandler.ReadBytes(Buf, Size, False);
>
> Use ReadStream() instead:
>
>    fMem.Clear;
>    IOHandler.ReadStream(fMem, Size, False);
>
>>         BytesToRaw(Buf, fMem, Size);
>>         fMem.Seek(0,soFromBeginning); <==== I get a EAccessViolation.
>
> You are trying to write your data to the memory address where the stream
> object itself resides, not to the memory address where the stream stores
> its
> data buffer.  You are not writing the data into the stream at all.  To do
> that, you would have to set the stream's Size property (to preallocate the
> buffer to the proper size), and then write the data into the stream's
> Memory
> property, ie:
>
>    fMem.Size := Size;
>    BytesToRaw(Buf, fMem.Memory, Size);
>
>> I seem to always end up with problems using streams
>
> You aren't using them properly.
>
>
> Gambit

Replies:

In response to:

www.cryer.info
Managed Newsgroup Archive