Re: Question abut threads

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



On Fri, 08 Aug 2008 14:19:23 -0700, markgoldin <markgoldin_2000@xxxxxxxxx> wrote:

Yes, each listener listen to a different port.
I am using 8 ports for now.

Any particular reason why? One port should be sufficient, assuming the rest of your code is designed correctly.

For sure, you've got at least one serious bug:

[...]
i = stream.Read(bytes, 0, bytes.Length);

while (i > 0)

{

i = stream.Read(bytes, 0, bytes.Length);

Whatever bytes you read from the stream in your first call to Read(), you simply discard. I'm surprised that you get _any_ successful results with this code. Maybe you have some bug elsewhere that somehow offsets this bug.

And here's a line of code that could be bug if it was extrapolated to other encodings:

// Translate data bytes to a ASCII string.

data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);

I'd have to spend a little more time reviewing the Encoding class stuff, but my recollection is that you need to get a single Encoding instance and use it repeatedly when decoding strings from a byte stream. Otherwise, if you receive partial data, it will get lost between calls to GetString().

Since ASCII is always only one byte per character, this isn't a problem with ASCII per se. But if you were to change your design to transmit encodings it would be.

But for now, the main thing you need to fix is to stop discarding the first sequence of bytes you receive. As a minimal change to your code, I'd suggest something like this:

i = stream.Read(bytes, 0, bytes.Length);

while (i > 0 && !data.EndsWith("\r"))
{
// Translate data bytes to a ASCII string.

data += System.Text.Encoding.ASCII.GetString(bytes, 0, i);

i = stream.Read(bytes, 0, bytes.Length);
}

You may also want to consider changing your code so that it uses a StringBuilder to accumulate the string, rather than repeatedly accumulating the string being read in. Whether it's really a problem depends on how many times you're actually doing to concatenate and how long the string will eventually get. But for large strings, StringBuilder will be much more efficient.

Pete
.



Relevant Pages

  • Re: Question abut threads
    ... I dont have any particular reason for 8 ports except that I have 8 clients ... TcpClient client = listener.AcceptTcpClient; ... encodings it would be. ... StringBuilder to accumulate the string, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: FAQ Topic - How can I create a Date object from a String? (2010-07-27)
    ... include something explicitly on writing& reading JSON date strings. ... ECMA-5 doesn't say that a Date string is parsed in UTC. ... The downside of browsers being released with a bug is that even after the browser vendor releases an update with a fix, there will still be some users that have an older browser with the broken functionality. ...
    (comp.lang.javascript)
  • Re: Implementing strstr
    ... Worst kind of bug: a bug that doesn't cause your code to fail. ... you use a scripting language with builtin string ... that pattern is a device for the user to abstract the name. ... fraud and a thief, Dweebach, and this issue is not going away. ...
    (comp.lang.c)
  • Re: [EGN] Variable hoisting
    ... Re: The Data Quality Act ... In the process I found a SERIOUS bug in the C code. ... CS> The allocated string has no room for the trailing nul. ... An mature adult programmer would have taken it for what ...
    (comp.programming)
  • Re: Comment on trim string function please
    ... I think you have a bug. ... but does it fly past the terminating null character? ... initial space scan) then this writes outside the string. ... ASIDE FROM THE CAST "ISSUES", ...
    (comp.lang.c)