Problem using BinaryReader

P

Pedro Sousa

Hi,

I'm using a TCP connection for sending text commands and receive text
answers from a server using a proprietary protocol. In one of the
responses the server sends binary data from an image file.
I'm using StreamReader.ReadLine() method for the general message
receiving, but when the binary data comes, it should be read with
BinaryReader.Read(...) method.

I'm only creating the BinaryReader from the underlying NetworkStream
whenever I know that the binary data is arriving. The binary data is
preceeded by the string "data=", which I also try to read using the
BinaryReader's Read(...) method, and the size of the image data
(number of bytes) has been sent earlier by the server.

When running on the full desktop framework, I can successfully read
the sentence "data=" from the stream using the BinaryReader (converted
from bytes to string using the ASCII encoding). However, when running
under the Compact Framework (either in the Emulator or in the device
itself) I always read 5 "garbage bytes" instead of "data="...

What am I doing wrong? Is there some kind of problem in the alternate
use of StreamReader and BinaryReader on the same underlying
NetworkStream? Should the BinaryReader be instantiated earlier?


Thanks in advance,
 
A

Alex Feinman [MVP]

If you are creating your binary reader both on the destop and device without
specifying the encoding, desktop will use ASCII and device will use Unicode.
Try explicitly specifying the same encoding (e.g. Encoding.ASCII) in
BinaryWriter and BinaryReader constructors on the desktop and device
respectively
 
G

Guest

Alex Feinman said:
If you are creating your binary reader both on the destop and device without
specifying the encoding, desktop will use ASCII and device will use Unicode.
Try explicitly specifying the same encoding (e.g. Encoding.ASCII) in
BinaryWriter and BinaryReader constructors on the desktop and device
respectively

Hello again.

In fact I wasn't specifying the Encoding in none of the cases (StreamReader
and BinaryReader), but even when I set them both to ASCII (I even tried other
encoding combinations) the result is the same: I get the same garbage bytes
instead of 'data='.

Is there anything else I might try? I'm not having any great ideas...

Thanks,
 
A

Alex Feinman [MVP]

Could you modify your code temporarily to save the bytes coming from the
networkstream to a byte array and post these bytes?
 

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