Article View: borland.public.cppbuilder.internet.socket
Article #4021Re: how to create XMLDocument from text string
From: Remy Lebeau (TeamB)
Date: Wed, 27 Apr 2005 09:54
Date: Wed, 27 Apr 2005 09:54
57 lines
3506 bytes
3506 bytes
"Daniel Stevenson" <daniel@ocreage.com.au> wrote in message
news:426f30b1@newsgroups.borland.com...
> I'm using BCB6, i have an application with a TServerSocket, I am
> being sent XML to my socket and I am struggling to turn the text
> string into a valid XML document using TXMLDocument.
Your first mistake is that you are not using TXMLDocument properly to begin
with. You do not use the XML property to load string data into it. Use the
LoadFromXML() method instead:
XML->LoadFromXML(RxBuffer);
With that said, TXMLDocument does not work properly when instantiated
dynamically anyway, so use the IXMLDocument interface directly
instead:
_di_IXMLDocument XML = LoadXMLData(RxBuffer);
// use XML as you normally would, but do not free it, it is freed
automatically...
> void __fastcall TMyService::SocketRead(TObject *Sender,
TCustomWinSocket *Socket)
> {
> AnsiString RxBuffer = Socket->ReceiveText();
You are making a very bad assumption there. You are assuming that
ReceiveText() will return all of your XML data in full in a single read.
Chances are, that is not the case at all. Very commonly, TCP sockets break
up data packets, and also merge multiple packets together. It is your own
responsibility to determine where one packet ends and the next one begins,
and then only parse data when you know you actually have a full packet
available. You have two choices:
1) place a delimiter between your XML strings, some value that is quaranteed
never to appear in the XML itself, such as a null character. Read all
received data until you encounter a null character and then parse everything
received before it.
2) preceed each string value with a numeric value that specifies the length
of the string. Read the length first, then you can read and process the
following string data afterwards.
In either case, since you appear to be using the socket in non-blocking
mode, you will have to buffer the received data in memory somewhere and not
operate on the data right away. The OnRead event does not guarantee how
much data you will actually receive each time it is trggered. You may
receive partial packets, or even multiple packets at one time. Keep
buffering the data, parsing only complete packets when they are actually
available. If you need examples, the issue has already been discussed many
times before, so go to http://www.deja.com to search through the newsgroup
archives. Sameple code and detailed descriptions have been given many many
times.
Gambit
Technical Details
Message-ID:
<426f60d4$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<426f30b1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,202e5b119b206323 X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.internet.socket References: <426f30b1@newsgroups.borland.com> Subject: Re: how to create XMLDocument from text string Date: Wed, 27 Apr 2005 02:54:46 -0700 Lines: 59 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 68.69.17.41 Message-ID: <426f60d4$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1114595540 68.69.17.41 (27 Apr 2005 02:52:20 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!proxad.net!194.117.148.138.MISMATCH!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail