473,803 Members | 3,857 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("host name", port_no);
NetworkStream stream = client.GetStrea m();
StreamReader sIn = new StreamReader(st ream, new
System.Text.UTF 8Enconding(fals e));
XmlTextReader xIn = new XmlTextReader(s In);

while (true) {
String stanza = xIn.ReadOuterXm l(); // this blocks till
data following stanza
}
}
}
or then replace the "while" block in the above with this

xIn.Read();
while (true) {
if (xIn.IsStartEle ment()) {
XmlReader inner = xIn.ReadSubtree ();
inner.Read();
String stanza = inner.ReadOuter Xml(); // this
sometimes (< .01%) blocks
inner.Close();
xIn.Read();
} else
xIn.Read();
}
Does anyone have any insights?

Thanks,
Greg

Feb 22 '06 #1
3 4595
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

<g6*******@hotm ail.com> wrote in message
news:11******** *************@o 13g2000cwo.goog legroups.com...
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("host name", port_no);
NetworkStream stream = client.GetStrea m();
StreamReader sIn = new StreamReader(st ream, new
System.Text.UTF 8Enconding(fals e));
XmlTextReader xIn = new XmlTextReader(s In);

while (true) {
String stanza = xIn.ReadOuterXm l(); // this blocks till
data following stanza
}
}
}
or then replace the "while" block in the above with this

xIn.Read();
while (true) {
if (xIn.IsStartEle ment()) {
XmlReader inner = xIn.ReadSubtree ();
inner.Read();
String stanza = inner.ReadOuter Xml(); // this
sometimes (< .01%) blocks
inner.Close();
xIn.Read();
} else
xIn.Read();
}
Does anyone have any insights?

Thanks,
Greg

Feb 23 '06 #2
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

Feb 23 '06 #3
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

Feb 26 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1685
by: Stelrad Doulton | last post by:
Hi all, I am writting a Jabber-esque client-server application and have run into an issue with the XmlTextReader constructor when passing a NetworkStream - It hangs forever, apparently this is an issue with SP1. Anyway I have the code below as a work around (using bool keepalives as it must run on the CF): while(keepAlive)
1
4785
by: Casey Watson | last post by:
Hi :) I'm having some major trouble with an XML Client/Server application that I am writing. I am using NetworkStream with CryptoStream to Read and Write XML between computers. Now, whenever I write the XML, the Server section which uses the XMLTextReader Class will not accept EndElements period, and neither the ReadStartElement or ReadEndElement will work properly. However, if I only send to WriteStartElements over the network, the...
1
2518
by: Borgbjerg | last post by:
I've got a multi-threaded server (we are very early in the project, trying to uncover some risks), and I want to send data using XML. The class ServerAdapter is supposed to connect to the server (which works fine), to send data, but on the same stream be able to read data (which is causing problems). In the function "bool Connect()", we want to instantiate the XmlTextReader with the stream that we got from the 'TcpClient client', but...
0
1533
by: Abubakar | last post by:
Hi, try { int x = ns.Read(readbuffer, 0, readbuffer.Length); } catch (System.IO.IOException ioexception) { UINotifications.ServerMessageDisplay(ioexception.ToString( )); }
6
3329
by: Ryan | last post by:
Hi, I am confused with how NetworkStream works. My application needs to handle heavy requests sent through TCP socket connection. I use NetworkStream.Read method to get the stream data. The
4
1275
by: Tom Shelton | last post by:
Problem: XmlTextReader Constructor hangs until underlying socket closes when passed a NetworkStream. Example: Dim reader As New XmlTextReader (New NetworkStream(s, False)) I can positively confirm by the way that this only happens AFTER installation of SP1 for framework 1.1. So, has anyone else have this
4
7576
by: DaveR | last post by:
I have a background thread to accept and process TCP/IP connections. When the application shuts down I want to gracefully terminate it. The thread will block in NetworkStream.Read() waiting for data. I expected that shutting down the TcpListener would unblock the Read() and cause an exception, or return 0 bytes. But it does not work. Relevant parts of the code are shown below. If a client connects, the stream blocks in its Read()...
2
4104
by: PiotrKolodziej | last post by:
Hi I have a simple question. Here is the code related to my question: while (true) { if (tcpListenerServer.Pending() && !this.Disposing) { TcpClient tcpClient = tcpListenerServer.AcceptTcpClient();
4
12804
by: Cyron | last post by:
Hello Friends, When I make a call to NetworkStream.Read(byte buffer, int offset, int size) I have found that this method will block until size bytes have been read or the connection is closed. Is this the correct behavior? My MSDN documentation led me to believe that this function should block until at least 1 byte of data was available, the connection was closed, or size bytes of data had been read. Any comments or clarifications...
0
9700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9564
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5498
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.