By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 880 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

XmlTextWriter & Socket

P: n/a
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();
}
}
}


Nov 12 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
K Rege wrote:
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.


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
Nov 12 '05 #2

P: n/a
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:
K Rege wrote:
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.


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

Nov 12 '05 #3

P: n/a
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);
}
}

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.