473,732 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Strea m
(a NetworkStream). It is indeed much easier to parse the headers with
a
simple StreamReader.Re adLine(). 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.Ba seStream (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 5015
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.Strea m
(a NetworkStream). It is indeed much easier to parse the headers with
a
simple StreamReader.Re adLine(). 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.Ba seStream (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.Strea m
(a NetworkStream). It is indeed much easier to parse the headers with
a
simple StreamReader.Re adLine(). 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.Ba seStream (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**@discussio ns.microsoft.co m> wrote in message
news:FF******** *************** ***********@mic rosoft.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.Strea m
> (a NetworkStream). It is indeed much easier to parse the headers with
> a
> simple StreamReader.Re adLine(). 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.Ba seStream (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
3254
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 lines I have to read several times and write them with other values. What is wrong with this code: using (StreamWriter swr = new StreamWriter(outPath))
5
3057
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 outer loop and the first StreamReader is still where I left it. I have been trying to do this using this code: StreamReader sr2 = sr1 //Where sr1 is the existing StreamReader This seems to work just fine, so I guess this is a supported way...
11
35781
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 use the seek() like this: StreamReader r; ....... r.BaseStream.seek(.....);
1
18394
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 or change the value of the Position property, but this does not seem to affect the Peek() method, which keeps returning -1 as if it had not been notified that the stream pointer has been repositioned to file beginning. How can I let Peek() work...
1
4668
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 MemoryStream(); StreamWriter streamWriter = new StreamWriter(stream, Encoding.ASCII); streamWriter.Write(text);
0
287
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 typical response in this protocol would be: SMP/1.0 200 OK<crlf> User-Agent: foo<crlf> Version: foo<crlf>
2
4469
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Ê%' \Y7". Here is my code segment written in VB.NET Enterprise Architect: -------- Public Function MD5File(ByVal Filename As String) As String ' ' Read a file, produce an MD5 hashed key value in a String.
9
5081
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 have to make the StreamReader jump to some point in the file. To make the StreamReader jump to where it should be, I use the following code: fReader.BaseStream.Seek(fileBookmark + (Me.reportTextWidth * linesPerPage * startPage),...
3
5233
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 there into StreamReader and StreamWriter objects. The StreamReader is working beautifully, but the StreamWriter isn't. If I convert the StreamWriter.BaseStream back to a FileStream and use its SafeFileHandle in the Windows API WriteFile...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.