c# TCP Server Client Threading Issies

1.7k views Asked by At

Here is the problem, I have below scenario coded.

  1. A TCP Listener running in thread A connecting to a Client 1
  2. A Second TCP Listener running in thread B Connecting to multiple clients each in a different thread. By this I mean as soon as a client is accepted in thread B, A different thread is created for further processing of the client where it waits to get data from "TCP Listener B" in a while loop

What I want to do and where I'm facing problem is,

I want to pass data recieved from Thread 1 by the Client 1 to clients in thread 2.

What I believe is happening,

Since I'm in Thread 1 when I get data from Client 1 in Thread 1, when I try sending data to clients in thread 2, I always get connection false.

Is this a threading issue?

How can I overcome it issue?

TCP Connection

EDIT

It's a windows form application

Both the TCP Listener run in same application.

I'm maintaining a list of TcpClients that connect to TCP Listener B and when I get some data from thread A, I get the TCP Client from this list that I maintain and try to send the data, but cannot as connection state becomes false.

2

There are 2 answers

0
Sebastian L On

I've had the same problem, so i used a backgroundworker instead of a thread to handle the Clients. Here is the extracted code from my project:

 private static void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        listener.Start();
        while (!bw.CancellationPending)
        {
            TcpClient client = listener.AcceptTcpClient();
            ThreadPool.QueueUserWorkItem(ThreadProc, client);
        }
        listener.Stop();
    }

And in ThreadProc its just handling every client

private static void ThreadProc(object state)
    {
        var client = (TcpClient)state;
        clientsList.Add(client);
        handleClient hclient = new handleClient();
        hclient.startClient(client); //Do whatever with your client here
    }

And to broadcast information i did this

public static void broadcast(byte[] msg, string uName)
    {
        foreach (TcpClient Item in clientsList)
        {
            if (Item != null)
            {
                try
                {
                    TcpClient broadcastSocket;
                    broadcastSocket = Item;
                    NetworkStream broadcastStream = broadcastSocket.GetStream();
                    Byte[] broadcastBytes = null;

                    broadcastBytes = msg;

                    Debug.WriteLine("Send Data to everyone from " + uName);
                    broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
                    broadcastStream.Flush();
                }
                catch (Exception)
                {
                    //Handle exceptions ....
                }

            }
        }
    }

i hope his helps. You can look at the finished project at https://github.com/hrkrx/MontagsmalerVS the examples are from the HostController.cs

0
Jorge Omar Medra On

As i can see, you are using threads and a Winform with socket servers and socket clients, and it could make that socket gets blocked.

Try to use a single thread to attend all your TCPListeners and TCPClients, using asynchronous methods which help to avoid socket blocking: