Connecting Tech Pros Worldwide Forums | Help | Site Map

XmlTextWriter & Socket

K Rege
Guest
 
Posts: n/a
#1: Nov 12 '05
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]
Guest
 
Posts: n/a
#2: Nov 12 '05

re: XmlTextWriter & Socket


K Rege wrote:
[color=blue]
> 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.[/color]

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]
http://blog.tkachenko.com
MadHatter
Guest
 
Posts: n/a
#3: Nov 12 '05

re: XmlTextWriter & Socket


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.

"Oleg Tkachenko [MVP]" wrote:
[color=blue]
> K Rege wrote:
>[color=green]
> > 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.[/color]
>
> 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]
> http://blog.tkachenko.com
>[/color]
oscarl@hotmail.com
Guest
 
Posts: n/a
#4: Nov 12 '05

re: XmlTextWriter & Socket


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);
}
}

Closed Thread