
February 22nd, 2006, 10:15 PM
|
|
|
XmlTextReader and NetworkStream blocking
Hi.
We have a Jabber-esque client server package that uses XMPP for
communication over network sockets.
Using .NET 2.0, I need to read a full stanza (i.e. balanced xml) as
soon as available and return it as a string.
So, I've taken my NetworkStream and wrapped an XmlTextReader around it.
At first I tried using ReadOuterXml() to grab the full stanza. This
worked fine except that it blocks until there is more data in the
stream behind the full stanza (since the ReadOuterXml() positions the
stream curser on the next element after the end element of the stanza).
This meant that the last message in would sit there until a new one
arrived...and this is not useful behaviour.
After some experimenting, I found that if I did a Read() on the start
element and then a ReadSubtree(), I could then do a ReadOuterXml() on
the subtrees reader and it returned the full stanza. The Read() blocks
until there is a new message, and the ReadSubtree() blocks until there
is a full stanza available.
However, the problem I've run into is that the ReadOuterXml() on the
subtree will block too, very occasionally (maybe .01% of the time, but
anything greater than 0% is unusable).
Is there a bug in ReadOuterXml()? Is there any other way (short of
writing my own parser) to grab full stanzas as strings?
Here is some sample code (C#) :
MyClass() {
static void Main(string[] args) {
TcpClient client = new TcpClient("hostname", port_no);
NetworkStream stream = client.GetStream();
StreamReader sIn = new StreamReader(stream, new
System.Text.UTF8Enconding(false));
XmlTextReader xIn = new XmlTextReader(sIn);
while (true) {
String stanza = xIn.ReadOuterXml(); // this blocks till
data following stanza
}
}
}
or then replace the "while" block in the above with this
xIn.Read();
while (true) {
if (xIn.IsStartElement()) {
XmlReader inner = xIn.ReadSubtree();
inner.Read();
String stanza = inner.ReadOuterXml(); // this
sometimes (< .01%) blocks
inner.Close();
xIn.Read();
} else
xIn.Read();
}
Does anyone have any insights?
Thanks,
Greg
|

February 23rd, 2006, 06:55 PM
|
|
|
Re: XmlTextReader and NetworkStream blocking
Hello Greg,
I don't see anything wrong with your code nor I am aware of any bug in
XmlTextReader or ReadOuterXml that could be causing the <.01% blocking.
Could it be because of some network issues?
Thanks,
-Helena Kotas, MS
<g66g08d14@hotmail.com> wrote in message
news:1140645977.066188.24310@o13g2000cwo.googlegro ups.com...[color=blue]
> Hi.
>
> We have a Jabber-esque client server package that uses XMPP for
> communication over network sockets.
>
> Using .NET 2.0, I need to read a full stanza (i.e. balanced xml) as
> soon as available and return it as a string.
>
> So, I've taken my NetworkStream and wrapped an XmlTextReader around it.
> At first I tried using ReadOuterXml() to grab the full stanza. This
> worked fine except that it blocks until there is more data in the
> stream behind the full stanza (since the ReadOuterXml() positions the
> stream curser on the next element after the end element of the stanza).
> This meant that the last message in would sit there until a new one
> arrived...and this is not useful behaviour.
>
> After some experimenting, I found that if I did a Read() on the start
> element and then a ReadSubtree(), I could then do a ReadOuterXml() on
> the subtrees reader and it returned the full stanza. The Read() blocks
> until there is a new message, and the ReadSubtree() blocks until there
> is a full stanza available.
>
> However, the problem I've run into is that the ReadOuterXml() on the
> subtree will block too, very occasionally (maybe .01% of the time, but
> anything greater than 0% is unusable).
>
> Is there a bug in ReadOuterXml()? Is there any other way (short of
> writing my own parser) to grab full stanzas as strings?
>
> Here is some sample code (C#) :
>
> MyClass() {
> static void Main(string[] args) {
> TcpClient client = new TcpClient("hostname", port_no);
> NetworkStream stream = client.GetStream();
> StreamReader sIn = new StreamReader(stream, new
> System.Text.UTF8Enconding(false));
> XmlTextReader xIn = new XmlTextReader(sIn);
>
> while (true) {
> String stanza = xIn.ReadOuterXml(); // this blocks till
> data following stanza
> }
> }
> }
>
>
> or then replace the "while" block in the above with this
>
> xIn.Read();
> while (true) {
> if (xIn.IsStartElement()) {
> XmlReader inner = xIn.ReadSubtree();
> inner.Read();
> String stanza = inner.ReadOuterXml(); // this
> sometimes (< .01%) blocks
> inner.Close();
> xIn.Read();
> } else
> xIn.Read();
> }
>
>
> Does anyone have any insights?
>
> Thanks,
> Greg
>[/color]
|

February 23rd, 2006, 08:45 PM
|
|
|
Re: XmlTextReader and NetworkStream blocking
Hi Helena,
Thank you for your response.
Interestingly enough, I was able to get the problem to disappear (by
fluke really). Here's what changed.
Before, the code in the 2nd block (that uses the subtree) was actually
running in its own thread. So the main thread did all the socket
connection and stuff and then spawned a thread that did the reading of
the stream.
Now, I've added one level of threading. So, the main thread spawns a
thread that does all the connection stuff and that spawned thread
spawns another to do the reading. Other than that, the code is
unchanged. But now I do not see the <.01% blocking anymore.
Of course...this makes me a little nervous since it should not have
made any difference.
Any thoughts on why this change would stop the problem from happening?
Thanks,
Greg
|

February 26th, 2006, 08:15 PM
|
|
|
Re: XmlTextReader and NetworkStream blocking
Hi Greg,
not sure if your problem is related to the following. WIth .NET 1.1 SP1
Microsoft made changes in the XMLTextReader. So with 1.1 SP1 you are
not able to read networkstreams correctly. Here is a big thread about
the problem:
http://mail.jabber.org/pipermail/jab...hread.html#342
Would be interesting to know if this is fixed this is 2.0, or if there
is the same problem.
Alex
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|