473,249 Members | 1,674 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,249 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 4960
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 "LzrV%'...
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: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.