Newsgroups : Borland : borland.public.delphi.internet.winsock : 2007 Dec : Re: idTcpClient and Streams

www.cryer.info
Managed Newsgroup Archive

Re: idTcpClient and Streams

Subject:Re: idTcpClient and Streams
Posted by:"Kendrick" (kkendri..@harbornet.com)
Date:Thu, 6 Dec 2007 09:37:15

Here are some cleaner snippets. Using the XML formats I can see the stream
in the memo and all of  the xml makes it to the client but it will not load
into the clinet's clientdataset. There is a test in snip 3 that creates the
steream and reloads on the server and that works with all formats. Thanks
for taking a look - Kevin


Snip 1: Server creates stream
//client data set cds1 is filled and active
... try
      if aStream = nil then aStream:= TMemoryStream.Create;
      aStream.Clear;
      aStream.Position:= 0;
      case rgDataType.ItemIndex of
        0: cds1.SaveToStream(aStream,dfBinary);
        1: cds1.SaveToStream(aStream,dfXML);
        2: cds1.SaveToStream(aStream,dfXMLUTF8);
      end;
      aStream.Write(buf,8); //this just adds extra blank spaces. Buf is an
array of char filled with spaces
      aStream.Position:= 0;
      SendClientMessage(aMsg, IntToStr(aStream.Size), False, True); //This
lets the client know how big the steam is with buf included
      Memo1.Lines.LoadFromStream(aStream); //This is just so I can see it on
the Server side
    except
      PostDBugMessage(Integer(aMsg.WParam),
        'Error in Stream building ' + IntToStr(aStream.Size ));
    end;


Snip 2: Client request for the stream. I am no longer use the WorkEnd event.

procedure TForm2.sbGetClick(Sender: TObject);
begin
  try
    Timer1.Enabled:= False; //this timer when running keeps checking for
socket messages via Readln
    //tcp1.ReadTimeout:= 10000; //just something I tried did not seem to
matter
    tcp1.IOHandler.WriteLn('SENDDATA');
    tcp1.IOHandler.ReadStream(aStream,StreamSize);// StreamSize includes the
extra blank buf bytes
    try
     cds.Active:= False;
      aStream.Position:= 0;
      Memo1.Lines.LoadFromStream(aStream); //just to give a visual
      aStream.Position:= 0;
      cds1.LoadFromStream(aStream);// this is where a data packet mismatch
occurs, any format binary or XML
      try
        cds1.Active:= True;
      except
        Memo1.Lines.Add('exception Saving stream');
      end;
    except
      Memo1.Lines.Add('exception Reading stream');
    end;
  finally
    Timer1.Enabled:= True;
    tcp1.ReadTimeout:= 100; //back to default
    aStream.Clear;
  end;
end;

Snip 3: Test routine in Server to create and read stream without tcp
transmission. This works just fine including with the extra white space
bytes.

procedure TForm1.TestDataStream;  {sends WM_RUN message without a client
connected}
var str:PChar; cHnd: integer;
begin
  cHnd:= 101;  {just a fake client handle}
  try
    str:= StrNew(PChar(mSql.Lines.Text)); {pointer to heap with the sql}
    Memo1.Lines.Clear;
    PostMessage(Handle, WM_RUN,cHnd,Integer(str)); //this is the event coded
in snip 1
    Application.ProcessMessages;
    cds1.Active:= False;
    aStream.Position:= 0;
    cds1.LoadFromStream(aStream);{already populated by the call to RUN}// no
error raised
    cds1.Active:= True;
  except
    on E: Exception do
      PostDBugMessage(cHnd,'TestDataStream '
                  + E.ClassType.ClassName + ': ' + E.Message);
  end;
end;

Replies:

none

In response to:

www.cryer.info
Managed Newsgroup Archive