471,309 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,309 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 4793
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Dan | last post: by
2 posts views Thread by Shaun Merrill | last post: by
9 posts views Thread by lord.zoltar | last post: by
reply views Thread by rosydwin | last post: by

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.