On Tue, 19 Aug 2008 10:46:04 -0700, Markgoldin <ma*************@yahoo.com>
wrote:
Here is my complete project:
Surely whatever problems exist in your code, they could be demonstrated
with just one TcpListener, rather than nine. Or, in other words, the code
you posted isn't a "concise-but-complete" code sample, which is the usual
standard for posting code when you want someone else to find problems in
it.
So, keeping in mind that I haven't put a lot of effort into examining each
and every line of code you posted...
[...]
Three problems with.Net side:
1. Sometimes it crashes
That's too vague a problem description for anyone to help. What does
"crash" mean? Does the OS actually terminate the process? Is there an
error or exception? If so, what is it?
2. when a client is disconnected CPU on a box that run this console
application goes to 100%.
Ahh...this seems familiar. I'm guessing this is the network code you had
questions about previously?
As far as the #2 question goes, you have some irregularities in your
SocketToLightStreamer.Run() method, but the one that is causing the CPU
consumption is that after the stream is closed (Read() returns 0), you
should set your TcpClient variable back to null.
It's not a great way to control flow within the loop, but that should at
least fix that issue, without having to resort to:
I am trying to handle that with this:
...
if (data.IndexOf("close") != -1)
{
System.Console.WriteLine("Closing connection ...");
go = false;
tcpl.Stop();
tcpl.Start();
new Thread(delegate() { Run(go, tcpl); }).Start();
return;
}
As before, I still fail to see the point in having a variable (named "go")
to control the loop. Especially as you set it to "true" as the first
thing in the Run() method and the only other place it ever gets set to
"false" is shortly before a "return" statement, what's the point?
Also, stopping and starting the TcpListener is pointless, and could in
fact cause issues because of the way that the OS deals with TCP listening
sockets (you may find that the port you're trying to use is unavailable
for a short time after having closed the listening socket, which is what
TcpListener does implicitly).
But the main question is: are you sure that a closed connection will have
written the text "close" to the stream before it is closed? Assuming that
it does, then I don't see why the above wouldn't work, even if it is a bit
clumsy. So it seems that the most likely explanation for your high CPU
utilization issue is that the stream doesn't have the text "close" in it
before it's actually closed.
3. I am missing some readings from sockets. I think that is due to this:
....
else
{
System.Console.WriteLine(data);
IDictionary eventData = new Hashtable();
eventData["scan"] = data;
_listener.Update("floorupdate", eventData, false);
}
I am thinking to move that into a separate thread. Do you think it will
help?
Honestly, the last thing I think that would help is _more_ threads. :)
But regardless, the code above doesn't appear to me to have anything to do
with the socket i/o at all. As near as I can tell, it's just how you're
passing the most-recently-received data to your IItemEventListener
implementation. So I don't think any modification of that section of
code, whether putting it in a new thread or something else, would help
whatever the "missing some readings from sockets" problem is.
All that said, there's really no real diagnostics that anyone can do with
respect to your code without a concise-but-complete code sample. For
network code, that means code for both ends of the TCP connection, along
with some predefined data demonstrating how the network connection is used.
Just want to remind I am not a c# coder. So dont kill if you see
something
funny :)
Just to be clear: we may harass, laugh at, or otherwise bother someone who
posts funny code (though normally we just try to help), but there's a
strict no-kill policy around here. As far as I know, no one has ever
actually lost their life because of something they posted here. :)
Pete