Problem accessing a StreamReader's BaseStream

A

Arno

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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 said:
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
 
G

Guest

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 ) said:
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 said:
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
 
I

Ignacio Machin \( .NET/ C# MVP \)

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 said:
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 ) said:
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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top