473,382 Members | 1,367 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

XmlTextWriter & Socket

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
3 3411
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
1
by: Jamie Saker | last post by:
I think I'm overlooking something assumed in socket's makefile method. Googling several hours and digging thru the python reference didn't help - I think I'm overlooking an assumption between...
5
by: Jain, Pranay Kumar | last post by:
Hello Everyone, I have written a simple app. that converts the dataset into excelspreadsheet. The App. uses the following architecture. First it generates the dataset with corresponding...
0
by: Andrew R. Thomas-Cramer | last post by:
I'm using XMLTextWriter to write to a socket stream. I need to write the opening root tag immediately for receipt by the client; elements beneath it will come later. ...
2
by: Dmitry Markin | last post by:
I've faced one cunning problem recently: I'm using IIS 6.0 as a web server, running on 1.1 framework. When i'm accessing my .net web service all goes fine, until I receive "The operation has...
2
by: Random | last post by:
Why, oh why, won't my XmlTextWriter write properly to the MemoryStream. Or, why can't I read the xml back out of the MemoryStream! I can't think of a thing.... Dim reader As XmlReader Dim...
11
by: atlaste | last post by:
Hi, In an attempt to create a full-blown webcrawler I've found myself writing a wrapper around the Socket class in an attempt to make it completely async, supporting timeouts and some scheduling...
6
by: clintonG | last post by:
Can anybody make sense of this crazy and inconsistent results? // IE7 Feed Reading View disabled displays this raw XML <?xml version="1.0" encoding="utf-8" ?> <!-- AT&T HTML entities & XML...
1
by: anilsahu | last post by:
Hi, I have a doubt related to xml. I want to print the copyright “©” symbol … but I wan to do it programmatically and without using the symbol directly. Hence I am using the ASCII value (© ) for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.