Re: Which send and recieve methods to use?

Giganews Newsgroups
Subject: Re: Which send and recieve methods to use?
Posted by:  Remy Lebeau (TeamB) (gambit…@yahoo.com)
Date: Mon, 29 Sep 2003

"John Small" <jo…@small8242.fsnet.co.uk> wrote in message
news:BC770F2AB480E240jo…@small8242.fsnet.co.uk...
> Indy provides a wide variety of send and receive methods
> for different purposes and for a novice it can be a little hard
> to work out which are the right ones to use.

There is no right or wrong way.  Like you said, different methods are for
different purposes, so it depends on what exactly you want to do.

> The program needs to send and receive XML packets, some of
> them are quite large so using line based send/receive isn't viable as
> it'll hit the end of the line.

That statement does not make any sense.  There is no problem sending XML in
a line-based protocol.  Why would you think that?  Please elaborate.

In any case, I would suggest using 1) Read/WriteInteger() with ReadBuffer()
and Write(), 2) Read/WriteStream(), or 3) Read/WriteStrings().  All of them
will allow you to send the data's length before the actual data, so that the
receiver can prepare its local buffer beforehand and then just fill it in as
the data arrives.  For example:

1)
    // server
    var XML: String;
    XML := whatever;
    ...WriteInteger(Length(XML));
    ...Write(XML);

    // client
    var XML: String;
    int iLen = ...ReadInteger;
    SetLength(XML, iLen);
    ...ReadBuffer(PChar(XML), iLen);

2)
    // server
    var Strm: TStringStream;
    var XML: String;
    XML := whatever;
    Strm := TStringStream.Create('');
    try
        Strm.WriteString(XML);
        ...WriteStream(Strm, True, True, 0);
    finally
        Strm.Free;
    end;

    // client
    var Strm: TStringStream;
    var XML: String;
    Strm := TStringStream.Create('');
    try
        ...ReadStream(Strm, -1, False);
        XML := Strm.DataString;
    finally
        Strm.Free;
    end;

3) (this works best if the XML is formatted to use line breaks in between
tags)
    // server
    var Strings: TStringList;
    var XML: String;
    XML := whatever;
    Strings := TStringList.Create;
    try
        Strings.Text = XML;
        ... WriteStrings(Strings, True);
    finally
        Strings.Free;
    end;

    // client
    var Strings: TStringsList;
    var XML: String;
    Strings := TStringList.Create;
    try
        ...ReadStrings(Strings, -1);
        XML := Strings.Text;
    finally
        Strings.Free;
    end;

> I can send a receive simple command response pairs, but when I
> try the full XML packet I get "line too long" or some corrupted data.

Sounds like you're trying to send the XML as a single line, and then setting
the AMaxLineLength parameter of ReadLn() too small.

Gambit

Replies

In response to

Which send and recieve methods to use? posted by John Small on Mon, 29 Sep 2003