Click here to Skip to main content
15,850,265 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi everyone,

I have a problem with a Threaded TCP Server. I use a listening Thread to handle all the incoming TCP connections. But, for some reason I ve got this messages from Windows XP at random times:

SCBC.exe has encountered a problem and needs to be shuted down

If I ignore this message the working threads (those which handle tcp conn) work fine, but I can't get another incoming tcp connection. I think the problem is the listening thread. It seems it crashed down at some point.

I start the listening thread with this:

private Thread TCPServer;
public static List<Thread> processor;

TCPServer = new Thread(delegate()
    new TCPServer();

This is the constructor for the TCP Server Obj:

//Some code which handles a database


This is my code for the StartListening Function:

public TcpListener listener;
public static List<TcpClient> lstClientsTCP = new List<TcpClient>();

        private void StartListening()
            int foo = 0;
            int bar = 0;


                lstClientsTCP = new List<TcpClient>();
                byte[] bytes = new Byte[1024];

                string Host = Dns.GetHostName();

                IPHostEntry IPs = Dns.GetHostByName(Host);
                IPAddress[] Direcciones = IPs.AddressList;

                IPAddress ipAddress = IPAddress.Parse(Direcciones[0].ToString());

                IPEndPoint localEndPoint = new IPEndPoint(ipAddress, listenport);

                while (true)
                    listener = new TcpListener(localEndPoint);

                    TcpClient client = listener.AcceptTcpClient();
                    client.ReceiveTimeout = 30000;


                    Thread tmpThread = null;
                    bar = foo;
                    tmpThread = new Thread(delegate() { ServiceClient(client, bar); });
                    if (foo == 999)
                        foo = 0;
            catch (Exception ex)
                Console.WriteLine("STARTLISTENING: " + ex.Message + "->" + ex.StackTrace);

DB object is an MySql Database handler object.

Am I doing something wrong? I ve been suggeste to use Thread Pools (use less memory), but I dont think thats the problem. If you have any ideas please let me know and excuse my english.



a few things look sketchy:
* with a while (true) loop how does the thread ever terminate? Please don't save that via Thread.Abort().
* there's no need to create a new TcpListener for every iteration of the loop. There only needs to be one instance created with Start called before the loop, and Stop needs to be called only once after the loop.
* that whole bit with foo, adding the thread to a list and rolling foo back over to 0 when it's 999 is entirely shady. If foo rolls over then that's an immediate bug right there. The call to Add will insert the new thread at index 998, processor[998].Start will be called, then foo rolls over. The next time through the loop the thread is added at index 999 but since foo rolled over processor[0].Start is being called again. Fail. All of this also assumes that no one is ever removing threads from the list, which should be happening somewhere once the threads have finished running to ensure that you don't run out of resources eventually. Also, do other threads access processor? If so then it needs to be protected with a lock statement.

If all else fails you should be able to use the Unhandled ExceptionHandler[^] to help find out the root cause of the problem.
Share this answer
the first thing to try is using SafeThread - Prevent Unhandled Exceptions in Secondary Threads[^] to make sure unhandled exceptions aren't the culprit.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900