473,394 Members | 1,785 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,394 software developers and data experts.

Problem accessing a StreamReader's BaseStream

Hi,

I'm using TcpClient for communication between two PC running a small
piece
of software. The protocol used has been designed internally and is
HTTP
similar (command line, headers, body). A typical response in this
protocol
would be:

SMP/1.0 200 OK<crlf>
User-Agent: foo<crlf>
Version: foo<crlf>
Content-Length: foo<crlf>
<crlf>
[BODY - binary or whatever]

To parse the response, I'm binding a StreamReader over the
TcpClient.Stream
(a NetworkStream). It is indeed much easier to parse the headers with
a
simple StreamReader.ReadLine(). Until now everything works fine.

But then, I need to read the body (the rest of the stream) using
binary
reading because it might well be a binary file. Trying to read
directly from
the NetworkStream or from the StreamReader.BaseStream (which are
supposedly
strictly the same) does not return anything. The buffer I use to read
remains
empty.

Why ? Is this that once a stream has been bound to a streamreader, I
MUST
use the streamreader until the end of the stream ?

To put it simply: my network stream has some content that should be
read as
text (Headers etc...) and another part that should be read as binary
content.

Any idea of how to implement that ?

Thanks in advance
Arno
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
3 4974
hi,

it.s possible that StreamReader has buffered part of the stream.

Take a look at the thread "Finding a StreamReader's true position" to see a
possible solution, keep using the NetworkStream and use the method I posted
there to read a string

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Arno" <ar************@caramail-dot-com.no-spam.invalid> wrote in message
news:42**********@127.0.0.1...
Hi,

I'm using TcpClient for communication between two PC running a small
piece
of software. The protocol used has been designed internally and is
HTTP
similar (command line, headers, body). A typical response in this
protocol
would be:

SMP/1.0 200 OK<crlf>
User-Agent: foo<crlf>
Version: foo<crlf>
Content-Length: foo<crlf>
<crlf>
[BODY - binary or whatever]

To parse the response, I'm binding a StreamReader over the
TcpClient.Stream
(a NetworkStream). It is indeed much easier to parse the headers with
a
simple StreamReader.ReadLine(). Until now everything works fine.

But then, I need to read the body (the rest of the stream) using
binary
reading because it might well be a binary file. Trying to read
directly from
the NetworkStream or from the StreamReader.BaseStream (which are
supposedly
strictly the same) does not return anything. The buffer I use to read
remains
empty.

Why ? Is this that once a stream has been bound to a streamreader, I
MUST
use the streamreader until the end of the stream ?

To put it simply: my network stream has some content that should be
read as
text (Headers etc...) and another part that should be read as binary
content.

Any idea of how to implement that ?

Thanks in advance
Arno
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com

Nov 16 '05 #2
Many thanks Ignacio for your explanation.

It is indeed the case (StreamReader buffering part of the data). I'll you
your piece of code to perform a line read from a simple network stream as
explained in the thread "Finding a StreamReader's true position".

My only concern is that performanced might be a bit less good (reading
characters byte per byte and testing each time the nature of this byte to
detect the end of the line), anyhow, I'll see how it turns to be.

Thanks again.
Cheers,
Arno

"Ignacio Machin ( .NET/ C# MVP )" wrote:
hi,

it.s possible that StreamReader has buffered part of the stream.

Take a look at the thread "Finding a StreamReader's true position" to see a
possible solution, keep using the NetworkStream and use the method I posted
there to read a string

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Arno" <ar************@caramail-dot-com.no-spam.invalid> wrote in message
news:42**********@127.0.0.1...
Hi,

I'm using TcpClient for communication between two PC running a small
piece
of software. The protocol used has been designed internally and is
HTTP
similar (command line, headers, body). A typical response in this
protocol
would be:

SMP/1.0 200 OK<crlf>
User-Agent: foo<crlf>
Version: foo<crlf>
Content-Length: foo<crlf>
<crlf>
[BODY - binary or whatever]

To parse the response, I'm binding a StreamReader over the
TcpClient.Stream
(a NetworkStream). It is indeed much easier to parse the headers with
a
simple StreamReader.ReadLine(). Until now everything works fine.

But then, I need to read the body (the rest of the stream) using
binary
reading because it might well be a binary file. Trying to read
directly from
the NetworkStream or from the StreamReader.BaseStream (which are
supposedly
strictly the same) does not return anything. The buffer I use to read
remains
empty.

Why ? Is this that once a stream has been bound to a streamreader, I
MUST
use the streamreader until the end of the stream ?

To put it simply: my network stream has some content that should be
read as
text (Headers etc...) and another part that should be read as binary
content.

Any idea of how to implement that ?

Thanks in advance
Arno
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com


Nov 16 '05 #3
Hi,

In my situation it does work fine, remember that you only needs to use that
code when you are expecting text commands, whenthe binary part start you do
not have to use it.
I use it on a similar situation, a protocol to send/receive files and once
I know there is a binary file coming in I use another code ( I can give you
it if you need it)
It does run in a pocketPC app btw.

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Arno" <Ar**@discussions.microsoft.com> wrote in message
news:FF**********************************@microsof t.com...
Many thanks Ignacio for your explanation.

It is indeed the case (StreamReader buffering part of the data). I'll you
your piece of code to perform a line read from a simple network stream as
explained in the thread "Finding a StreamReader's true position".

My only concern is that performanced might be a bit less good (reading
characters byte per byte and testing each time the nature of this byte to
detect the end of the line), anyhow, I'll see how it turns to be.

Thanks again.
Cheers,
Arno

"Ignacio Machin ( .NET/ C# MVP )" wrote:
hi,

it.s possible that StreamReader has buffered part of the stream.

Take a look at the thread "Finding a StreamReader's true position" to see
a
possible solution, keep using the NetworkStream and use the method I
posted
there to read a string

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Arno" <ar************@caramail-dot-com.no-spam.invalid> wrote in message
news:42**********@127.0.0.1...
> Hi,
>
> I'm using TcpClient for communication between two PC running a small
> piece
> of software. The protocol used has been designed internally and is
> HTTP
> similar (command line, headers, body). A typical response in this
> protocol
> would be:
>
> SMP/1.0 200 OK<crlf>
> User-Agent: foo<crlf>
> Version: foo<crlf>
> Content-Length: foo<crlf>
> <crlf>
> [BODY - binary or whatever]
>
> To parse the response, I'm binding a StreamReader over the
> TcpClient.Stream
> (a NetworkStream). It is indeed much easier to parse the headers with
> a
> simple StreamReader.ReadLine(). Until now everything works fine.
>
> But then, I need to read the body (the rest of the stream) using
> binary
> reading because it might well be a binary file. Trying to read
> directly from
> the NetworkStream or from the StreamReader.BaseStream (which are
> supposedly
> strictly the same) does not return anything. The buffer I use to read
> remains
> empty.
>
> Why ? Is this that once a stream has been bound to a streamreader, I
> MUST
> use the streamreader until the end of the stream ?
>
> To put it simply: my network stream has some content that should be
> read as
> text (Headers etc...) and another part that should be read as binary
> content.
>
> Any idea of how to implement that ?
>
> Thanks in advance
> Arno
>
>
> Posted Via Usenet.com Premium Usenet Newsgroup Services
> ----------------------------------------------------------
> ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
> ----------------------------------------------------------
> http://www.usenet.com


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jaga | last post by:
Hi, how can I read the same passage in a textfile several times? I'm writing a little textgenerator. It reads lines from a file, replaces the variables, and writes it in an other file. Some...
5
by: Anders Olsson | last post by:
I need to create a StreamReader which starts from the position of an existing StreamReader. This way, the second StreamReader can read lines "ahead" in an own loop, and then I can go back to the...
11
by: Tiger | last post by:
We can use seek() in the FileStream class,as we know. But I found that seek() is not work correctly in StreamReader. Who can tell me how to use seek() correctly in StreamReader? thanks a lot! I...
1
by: Dan | last post by:
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file and read it to end; then I'd want to move its stream pointer back to the file beginning: I can call BaseStream Seek method...
1
by: R.L. | last post by:
See the code below, var 'content ' is suppose to be "Hello!", not "". Who knows why? Thanks ---------------------------------------- string text = "hello!"; MemoryStream stream = new...
0
by: Arno | last post by:
Hi, I'm using TcpClient for communication between two PC running a small piece of software. The protocol used has been designed internally and is HTTP similar (command line, headers, body). A...
2
by: Shaun Merrill | last post by:
This function doesn't seem to be giving me the appearance I expect. This should appear more like "1ba442ab45c8002a86d068d1c1748e44" but it looks really cryptic and screwy, like "­óìØLÜzrVÊ%'...
9
by: lord.zoltar | last post by:
Hi, I'm having some trouble with a StreamReader. I use a System.IO.StreamReader to read a text file and then print it. If the user has selected a range of pages starting past the first page, I...
3
by: stumorgan | last post by:
I'm doing some USB communications in C# and am running into a minor annoyance. I'm using the Windows API CreateFile function to get a SafeFileHandle, which I then stuff into a FileStream and from...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.