Blocking StreamReader

===========
C#
Framework 1.1

===========

am writting a local proxy that listen for incoming http requests esp those sent/received through Yahoo Messenger to handle files transfer.

anyway, i have a test file (test.txt) which contains the following two lines:

Hello World
|

please note there's an empty line below Hello World's line(return). when i send the file through Yahoo Messenger, i get the final request from YM which is somthin like the following:

POSThttp://216.155.193.92/relay?...
Referer: ...
Cookie: ...
Host: 216.155.193.92
Content-Length: 11
Pragma: no-cache

Hello World

everythin is fine, i get the file properly. But when i edit the file(test.txt) and remove the empty line, my program get into blocking mode waitting for StreamReader to readline:

ns = new NetworkStream(client);
reader = new StreamReader(ns);
writer = new StreamWriter(ns);

string input = "";

while(reader.Peek() != -1)
input += reader.ReadLine() + "\n"; // waits forever

Console.WriteLine(input);

could somone explain that strange behaviour .. any comment is very apprechiated

[1454 byte] By [Spykeyz] at [2007-12-17]
# 1
It is understandable.
Your code is trying to do a readline().
Which means that you are looking for a \r\n or
the end of stream.
For files, since the end of stream is detectable, eventhough the
last line does not end with \r\n, the stream reader can detect that
and give you the last line.

For TCP Streams, unless the connection is closed, we don't know
that the stream is ended. So in this specific case the only
valid indication is the \r\n for readline to return and give you the data -
WHICH is NOT PRESENT.

So how to do you deal with this?
Thats exactly the purpose of the conten-length,
The right way is to do this following,

1. Look at the content length
2. Read exactly that many bytes
3. Look at the charset header [i think it is part of the content type header]
4. Depending on the charset, use the correct encoding to
convert back to unicode if you need to.
5. If you don't need to do UNICODE, just
do 1 and 2 and dump the data to a file.
Don't deal with strings - since you will never be sure that
it is always string data. If you force string conversion, some bytes
might get discarded.

DurgaprasadGorti at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
Thank you so much Durhaprasad, now its working just fine .. i really appreciate your help

regards,

Spy

Spykeyz at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified