Re: Question abut threads
- From: "User Groups" <markgoldin_2000@xxxxxxxxx>
- Date: Sat, 9 Aug 2008 05:34:43 -0500
Pete, thanks a lot for fixing my code. Really appreciate that.
I dont have any particular reason for 8 ports except that I have 8 clients
sending data to the .Net module.
These clients are scanner (data collection) servers. Users scan barcodes
which are going into different from .Net processes.
Then these processes send data to .Net module via sockets.
Creating separate ports for each scanning process should protect me from
concurrency problems when multiple users are scanning at the same time.
I have queuing mechanism on the data side too: I am trying not to send data
from multiple users at the same time, but for the sake of stability of the
program
I am also dedicating a port to each data stream.
protected void ListenerMethod()
{
TcpListener listener = new TcpListener(1212);
listener.Start();
while (true)
{
TcpClient client = listener.AcceptTcpClient();
new Thread(delegate { TheConnectionHandler(client); }).Start();
//new Thread(TheConnectionHandler).Start(client);
}
}
protected void TheConnectionHandler(object objArg)
{
TcpClient client = (TcpClient)objArg;
//...
}
I have a few questions about your code. How does exactly it work? Does it
create a new thread every time data has been sent?
Also where in the code do I extract data?
About having TcpClient client = listener.AcceptTcpClient(); inside of the
loop. I tried (before getting samples here) to have it inside, but
I wasn't eble to receive data more than once so I took it out of the loop.
Thanks again for all the help.
"Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx> wrote in message
news:op.ufk8hwx68jd0ej@xxxxxxxxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: Question abut threads
- From: Peter Duniho
- Re: Question abut threads
- References:
- Question abut threads
- From: Markgoldin
- Re: Question abut threads
- From: Ignacio Machin ( .NET/ C# MVP )
- Re: Question abut threads
- From: markgoldin
- Re: Question abut threads
- From: Peter Duniho
- Question abut threads
- Prev by Date: Re: Making a csharp Addin for Visual Studio dockable
- Next by Date: Re: Visual Studio versioning .... how to tell?
- Previous by thread: Re: Question abut threads
- Next by thread: Re: Question abut threads
- Index(es):
Relevant Pages
|
Loading