Groups | Blog | Home
all groups > dotnet xml > december 2004 >

dotnet xml : XmlTextWriter & Socket


K Rege
12/26/2004 4:42:58 PM
Hi there,

I tried to implement a little XML-EchoServer using Sockets and
XmlTextWriter/Reader. However the server hangs while reading the input from
the client (wr.Flush() seams not to work). I could make it run by using
s.Send(new byte[10000]); instead of flush, but this cannot be the solution.
Is there a better work around or fix to this problem? I use 1.1 with SP1, so
the earlier posting do not apply.

Merry Christmas and thanks

Karl

// Client ===========================================
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.IO;


namespace Kapitel4.Netzwerkkommunikation {

class EchoClient {

public static void Main() {
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Loopback, 5000));
Console.WriteLine("connected");
NetworkStream ns = new NetworkStream(s,true);
XmlTextWriter wr = new XmlTextWriter(ns,System.Text.Encoding.UTF8);

wr.WriteStartElement("World");
wr.WriteString("Hugo");
wr.WriteEndElement();
wr.Flush();
Console.WriteLine("sent");
////// get Answer
XmlTextReader rd = new XmlTextReader(ns);
rd.Read(); // starttag
rd.Read();
Console.WriteLine("read:"+rd.Value);
s.Close();
}
}
}



// Server ==========================================
using System;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Xml;
using System.IO;
namespace Kapitel4.Netzwerkkommunikation {

class EchoServer {

public void StartServer(){
IPAddress ip = IPAddress.Loopback;
int port = 5000;
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
s.Bind(new IPEndPoint(ip, port));
s.Listen(10);
while (true) {
Console.WriteLine("ready ...");
Socket nS = s.Accept();
Console.WriteLine("reading ...");
NetworkStream nwS = new NetworkStream(nS,true);
XmlTextReader rd = new XmlTextReader(nwS);
rd.Read(); // starttag
rd.Read();
string sh = rd.Value;
Console.WriteLine("writing ...");
XmlTextWriter wr = new
XmlTextWriter(nwS,System.Text.Encoding.UTF8);
wr.WriteStartElement("World");
wr.WriteString("Hello "+sh);
wr.WriteEndElement();
wr.Close();
Console.WriteLine("finished...");
}

}

public static void Main() {
EchoServer server = new EchoServer( );
server.StartServer();
}
}
}



Oleg Tkachenko [MVP]
12/27/2004 11:43:37 AM
[quoted text, click to view]

That sounds like having a lot to do with a bug in XmlTextReader
introduced in the SP1 code. Search this newsgroup for more info on the
way to workaround it.

--
Oleg Tkachenko [XML MVP]
MadHatter
1/20/2005 1:53:01 PM
I'm looking for more info on this XmlTextReader issue introduced in sp1 of
1.1. Is this documented anywhere, and if so can I get a link?

thanks.

[quoted text, click to view]
oscarl NO[at]SPAM hotmail.com
1/25/2005 1:26:15 PM
There is a bug with .NET SP 1.1 using XmlTextReader with networkStream.

It works for me:

MemoryStream ms = new MemoryStream();

StreamReader sr = new StreamReader(ns);

if (ns.DataAvailable)
{
char[] b = new char[512];
int nread = sr.Read(b, 0, 512);

ms.Write(System.Text.Encoding.ASCII.GetBytes(b, 0, nread), 0, nread);
ms.Seek(0, System.IO.SeekOrigin.Begin);

XmlTextReader reader = new XmlTextReader(ms);
if (reader.Read())
{
objXmlDocument.Load(reader);
}
}
AddThis Social Bookmark Button