Re: Truncated data using .NET 3.5 sockets VS 2008
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Mon, 24 Mar 2008 10:05:53 -0700
On Mon, 24 Mar 2008 08:55:19 -0700, Kenny D <kennydevries@xxxxxxxxxxx> wrote:
I am having trouble sending streams of data from one socket to another
using the following code.
When the transmitter is running on the same machine as the receiver,
the data transmits perfectly. When the transmitter runs on another
machine, the data gets truncated at semi random places.
You are using StreamReader.Peek() to decide whether you've reached the end of the stream. Except that, as I read the docs for StreamReader.Peek(), that's not what it's going to tell you. It will return -1 any time that there is no character _currently_ available. It doesn't tell you anything about whether a character _may_ become available in the future.
I recommend changing your code to read bytes, properly checking for the end of the stream by watching for a zero-byte read from the stream. When that happens, you will know that the connection has been closed, indicating the end of the stream, at which point you can decode the bytes you've received all at once.
I admit, I don't use StreamReader.Peek() and thus don't have first-hand experience with respect to what it really does. But the documentation certainly implies to me that you're using it wrong. And if that implication is correct, it would absolutely explain the behavior you're seeing.
Pete
.
- Follow-Ups:
- Re: Truncated data using .NET 3.5 sockets VS 2008
- From: Jon Skeet [C# MVP]
- Re: Truncated data using .NET 3.5 sockets VS 2008
- References:
- Truncated data using .NET 3.5 sockets VS 2008
- From: Kenny D
- Truncated data using .NET 3.5 sockets VS 2008
- Prev by Date: Disable DataGridView cell highlighting
- Next by Date: Re: College c# Project final report
- Previous by thread: Re: Truncated data using .NET 3.5 sockets VS 2008
- Next by thread: Re: Truncated data using .NET 3.5 sockets VS 2008
- Index(es):
Relevant Pages
|