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

why does StreamReader get stuck

P: n/a
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class HttpProcessor
{
private Socket s;
private BufferedStream bs;
private StreamReader sr;
private StreamWriter sw;
private String method;
private String url;
private String protocol;
private Hashtable hashTable;

public HttpProcessor(Socket s)
{
this.s = s;
hashTable = new Hashtable();
}

public void process()
{
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(bs);
sw = new StreamWriter(bs);

sr.Peek (); <--------------- stuck, or sr.Read (...) stuck too

parseRequest();
readHeaders();
writeURL();

s.Shutdown (SocketShutdown.Both);
s.Close ();
}

public void parseRequest()
{
String request = sr.ReadLine();
string[] tokens = request.Split(new char[]{' '});
method = tokens[0];
url = tokens[1];
protocol = tokens[2];
}

public void readHeaders()
{
String line;
while((line = sr.ReadLine()) != null && line != "")
{
string[] tokens = line.Split(new char[]{':'});
String name = tokens[0];
String value = "";
for(int i = 1; i < tokens.Length; i++)
{
value += tokens[i];
if(i < tokens.Length - 1) tokens[i] += ":";
}
hashTable[name] = value;
}
}

public void writeURL()
{
try
{
FileStream fs = new FileStream(url.Length <= 1 ? "index.html" :
url.Substring(1), FileMode.Open, FileAccess.Read);
writeSuccess();
BufferedStream bs2 = new BufferedStream(fs);
byte[] bytes = new byte[4096];
int read;
while((read = bs2.Read(bytes, 0, bytes.Length)) != 0)
{
bs.Write(bytes, 0, read);
}
bs.Flush ();
bs2.Close();
}
catch(FileNotFoundException)
{
writeFailure();
sw.WriteLine("File not found: " + url);
}
}

public void writeSuccess()
{
sw.WriteLine("HTTP/1.0 200 OK");
sw.WriteLine("Connection: close");
sw.WriteLine();
sw.Flush();
}

public void writeFailure()
{
sw.WriteLine("HTTP/1.0 404 File not found");
sw.WriteLine("Connection: close");
sw.WriteLine();
}
}

public class HttpServer
{

// ================================================== ==========
// Data

protected int port;

// ================================================== ==========
// Constructor

public HttpServer() : this(80)
{
}

public HttpServer(int port)
{
this.port = port;
}

// ================================================== ==========
// Listener

public void listen()
{
Socket listener = new Socket(0, SocketType.Stream, ProtocolType.Tcp);
listener.Bind (new IPEndPoint(IPAddress.Any, port));
listener.Blocking = true;
listener.Listen((int) SocketOptionName.MaxConnections);
while(true)
{
Socket s = listener.Accept();
HttpProcessor processor = new HttpProcessor(s);
Thread thread = new Thread(new ThreadStart(processor.process));
thread.Start();
}
}

// ================================================== ==========
// Main

public static int Main(String[] args)
{
HttpServer httpServer;
if(args.GetLength(0) > 0)
httpServer = new HttpServer (Int32.Parse (args[0]));
else
httpServer = new HttpServer();
Thread thread = new Thread(new ThreadStart(httpServer.listen));
thread.Start();
return 0;
}
}

-----------
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(bs);
sw = new StreamWriter(bs);

sr.Read (...) call gets blocked?! so does sr.Peek () ?!!!!!!
sw.Write (...) is okay

----

NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(ns); <---- notice: ns not bs
sw = new StreamWriter(bs);

sr.Peek and Read now okay

----
So NetworkStream + BufferedStream + StreamReader = hang? WHY? HELP!
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Astronomically Confused,

Most likely, it gets stuck because you are using a buffered stream
instance. You shouldn't use this for network streams, because you will
never get an end of stream indicator. Rather it is trying to read a block
from the stream, and since whatever you are connected to isn't going to send
anymore, it hangs.

Remove the BufferedStream, and you should be fine.

A buffered stream won't work here because you need markers in your
messages that require you to read byte by byte (or character by character)
to identify those markers. Some message formats prepend the length to the
message, or have the length of the message embedded in the message
somewhere. This allows you to read larger chunks later on. However, this
does not seem to be the case here.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Astronomically Confused" <Astronomically
Co******@discussions.microsoft.com> wrote in message
news:A2**********************************@microsof t.com...
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class HttpProcessor
{
private Socket s;
private BufferedStream bs;
private StreamReader sr;
private StreamWriter sw;
private String method;
private String url;
private String protocol;
private Hashtable hashTable;

public HttpProcessor(Socket s)
{
this.s = s;
hashTable = new Hashtable();
}

public void process()
{
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite,
false);
bs = new BufferedStream(ns);
sr = new StreamReader(bs);
sw = new StreamWriter(bs);

sr.Peek (); <--------------- stuck, or sr.Read (...) stuck too

parseRequest();
readHeaders();
writeURL();

s.Shutdown (SocketShutdown.Both);
s.Close ();
}

public void parseRequest()
{
String request = sr.ReadLine();
string[] tokens = request.Split(new char[]{' '});
method = tokens[0];
url = tokens[1];
protocol = tokens[2];
}

public void readHeaders()
{
String line;
while((line = sr.ReadLine()) != null && line != "")
{
string[] tokens = line.Split(new char[]{':'});
String name = tokens[0];
String value = "";
for(int i = 1; i < tokens.Length; i++)
{
value += tokens[i];
if(i < tokens.Length - 1) tokens[i] += ":";
}
hashTable[name] = value;
}
}

public void writeURL()
{
try
{
FileStream fs = new FileStream(url.Length <= 1 ? "index.html" :
url.Substring(1), FileMode.Open, FileAccess.Read);
writeSuccess();
BufferedStream bs2 = new BufferedStream(fs);
byte[] bytes = new byte[4096];
int read;
while((read = bs2.Read(bytes, 0, bytes.Length)) != 0)
{
bs.Write(bytes, 0, read);
}
bs.Flush ();
bs2.Close();
}
catch(FileNotFoundException)
{
writeFailure();
sw.WriteLine("File not found: " + url);
}
}

public void writeSuccess()
{
sw.WriteLine("HTTP/1.0 200 OK");
sw.WriteLine("Connection: close");
sw.WriteLine();
sw.Flush();
}

public void writeFailure()
{
sw.WriteLine("HTTP/1.0 404 File not found");
sw.WriteLine("Connection: close");
sw.WriteLine();
}
}

public class HttpServer
{

// ================================================== ==========
// Data

protected int port;

// ================================================== ==========
// Constructor

public HttpServer() : this(80)
{
}

public HttpServer(int port)
{
this.port = port;
}

// ================================================== ==========
// Listener

public void listen()
{
Socket listener = new Socket(0, SocketType.Stream,
ProtocolType.Tcp);
listener.Bind (new IPEndPoint(IPAddress.Any, port));
listener.Blocking = true;
listener.Listen((int) SocketOptionName.MaxConnections);
while(true)
{
Socket s = listener.Accept();
HttpProcessor processor = new HttpProcessor(s);
Thread thread = new Thread(new ThreadStart(processor.process));
thread.Start();
}
}

// ================================================== ==========
// Main

public static int Main(String[] args)
{
HttpServer httpServer;
if(args.GetLength(0) > 0)
httpServer = new HttpServer (Int32.Parse (args[0]));
else
httpServer = new HttpServer();
Thread thread = new Thread(new ThreadStart(httpServer.listen));
thread.Start();
return 0;
}
}

-----------
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(bs);
sw = new StreamWriter(bs);

sr.Read (...) call gets blocked?! so does sr.Peek () ?!!!!!!
sw.Write (...) is okay

----

NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(ns); <---- notice: ns not bs
sw = new StreamWriter(bs);

sr.Peek and Read now okay

----
So NetworkStream + BufferedStream + StreamReader = hang? WHY? HELP!

Nov 16 '05 #2

P: n/a
In your reply you suggested that we should not use BufferedStream with
NetworkStream because an end of buffer marker will not show up. This claim is
false because in my experiment, I am able to direct read a block of bytes
from the BufferedStream just fine.

It is when using a StreamReader linked to a BufferedStream does the blocking
problem occur. StreamReader's peek or read causes the system to infinitely
wait for incoming data. Perhaps StreamReader's implementation does expect an
end of buffer marker; should your explanation be applied here, then it would
be correct.

The source code to the StreamReader's implementation is not open-source so
the problem must be further isolated through experimentation.

I can tell you that for a client-side C# application the StreamReader on the
BufferedStream in fact works. In the server-side implimentation, the
StreamReader blocks forever on peeks or reads. Thus, my experiments point
that the problem has something to do with sockets created through the Listen
method.

My intuition leads me to think that this is a bug. It exists in both .NET
1.1 and 2.0 beta run-times. Please verify this problem when you get the
chance. I appreciate it.
Nov 16 '05 #3

P: n/a
Astronomically Confused,
just for your information, there are ways to have a look at the
StreamReader's implementation, try using Lutz Roeder's Reflector or have a
look at Rotor sources.
In my opinion, this is not a bug in the framework.

"Astronomically Confused" <Astronomically
Co******@discussions.microsoft.com> wrote in message
news:55**********************************@microsof t.com...
In your reply you suggested that we should not use BufferedStream with
NetworkStream because an end of buffer marker will not show up. This claim is false because in my experiment, I am able to direct read a block of bytes
from the BufferedStream just fine.

It is when using a StreamReader linked to a BufferedStream does the blocking problem occur. StreamReader's peek or read causes the system to infinitely
wait for incoming data. Perhaps StreamReader's implementation does expect an end of buffer marker; should your explanation be applied here, then it would be correct.

The source code to the StreamReader's implementation is not open-source so
the problem must be further isolated through experimentation.

I can tell you that for a client-side C# application the StreamReader on the BufferedStream in fact works. In the server-side implimentation, the
StreamReader blocks forever on peeks or reads. Thus, my experiments point
that the problem has something to do with sockets created through the Listen method.

My intuition leads me to think that this is a bug. It exists in both .NET
1.1 and 2.0 beta run-times. Please verify this problem when you get the
chance. I appreciate it.

Nov 17 '05 #4

P: n/a
Did you ever get this figured out? I'm having similiar issues.

--
Thanks
Joe
"Astronomically Confused" wrote:
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;

class HttpProcessor
{
private Socket s;
private BufferedStream bs;
private StreamReader sr;
private StreamWriter sw;
private String method;
private String url;
private String protocol;
private Hashtable hashTable;

public HttpProcessor(Socket s)
{
this.s = s;
hashTable = new Hashtable();
}

public void process()
{
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(bs);
sw = new StreamWriter(bs);

sr.Peek (); <--------------- stuck, or sr.Read (...) stuck too

parseRequest();
readHeaders();
writeURL();

s.Shutdown (SocketShutdown.Both);
s.Close ();
}

public void parseRequest()
{
String request = sr.ReadLine();
string[] tokens = request.Split(new char[]{' '});
method = tokens[0];
url = tokens[1];
protocol = tokens[2];
}

public void readHeaders()
{
String line;
while((line = sr.ReadLine()) != null && line != "")
{
string[] tokens = line.Split(new char[]{':'});
String name = tokens[0];
String value = "";
for(int i = 1; i < tokens.Length; i++)
{
value += tokens[i];
if(i < tokens.Length - 1) tokens[i] += ":";
}
hashTable[name] = value;
}
}

public void writeURL()
{
try
{
FileStream fs = new FileStream(url.Length <= 1 ? "index.html" :
url.Substring(1), FileMode.Open, FileAccess.Read);
writeSuccess();
BufferedStream bs2 = new BufferedStream(fs);
byte[] bytes = new byte[4096];
int read;
while((read = bs2.Read(bytes, 0, bytes.Length)) != 0)
{
bs.Write(bytes, 0, read);
}
bs.Flush ();
bs2.Close();
}
catch(FileNotFoundException)
{
writeFailure();
sw.WriteLine("File not found: " + url);
}
}

public void writeSuccess()
{
sw.WriteLine("HTTP/1.0 200 OK");
sw.WriteLine("Connection: close");
sw.WriteLine();
sw.Flush();
}

public void writeFailure()
{
sw.WriteLine("HTTP/1.0 404 File not found");
sw.WriteLine("Connection: close");
sw.WriteLine();
}
}

public class HttpServer
{

// ================================================== ==========
// Data

protected int port;

// ================================================== ==========
// Constructor

public HttpServer() : this(80)
{
}

public HttpServer(int port)
{
this.port = port;
}

// ================================================== ==========
// Listener

public void listen()
{
Socket listener = new Socket(0, SocketType.Stream, ProtocolType.Tcp);
listener.Bind (new IPEndPoint(IPAddress.Any, port));
listener.Blocking = true;
listener.Listen((int) SocketOptionName.MaxConnections);
while(true)
{
Socket s = listener.Accept();
HttpProcessor processor = new HttpProcessor(s);
Thread thread = new Thread(new ThreadStart(processor.process));
thread.Start();
}
}

// ================================================== ==========
// Main

public static int Main(String[] args)
{
HttpServer httpServer;
if(args.GetLength(0) > 0)
httpServer = new HttpServer (Int32.Parse (args[0]));
else
httpServer = new HttpServer();
Thread thread = new Thread(new ThreadStart(httpServer.listen));
thread.Start();
return 0;
}
}

-----------
NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(bs);
sw = new StreamWriter(bs);

sr.Read (...) call gets blocked?! so does sr.Peek () ?!!!!!!
sw.Write (...) is okay

----

NetworkStream ns = new NetworkStream(s, FileAccess.ReadWrite, false);
bs = new BufferedStream(ns);
sr = new StreamReader(ns); <---- notice: ns not bs
sw = new StreamWriter(bs);

sr.Peek and Read now okay

----
So NetworkStream + BufferedStream + StreamReader = hang? WHY? HELP!

Nov 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.