Home > Mobile >  C# check multiple client connections
C# check multiple client connections

Time:01-20

I am currently developing a one-to-many relationship between one server and many clients. Everytime a client connects I append to the client list: clientList.Add(client);

I wrote the following code to check the "pulse" of the client connection as to see if the client is still connected and currently updating the toolStripStatusLabel1

This seems to work well when checking one connection and in addition, I added the exception cleanup() when trying to send data so that it should check at all scenarios(Feel free to give your opinion).

My question now is, how do I check the connection from multiple clients?

Please find below the reference code:

private void StartListen()
        {
            //Creating a TCP Connection and listening to the port
            tcpListener = new TcpListener(System.Net.IPAddress.Any, 6666);
            tcpListener.Start();
            
            toolStripStatusLabel1.Text = "Listening on port 6666 ...";
            int counter = 0;
            appStatus = 0;

            
            while (true)
            {
                try
                {
                    client = tcpListener.AcceptTcpClient();
                    counter  ;
                    clientList.Add(client);
                    
                    
                    IPEndPoint ipend = (IPEndPoint)client.Client.RemoteEndPoint;
                    //Updating status of connection
                    toolStripStatusLabel1.Text = "Connected from "   IPAddress.Parse(ipend.Address.ToString());
                    appStatus = 1;
                    
                    th_outPutStream = new Thread(delegate () { outPutStream(client); });
                    th_outPutStream.Start();
                    th_inPutStream = new Thread(delegate () { inPutStream(client); });
                    th_inPutStream.Start();
                    th_checkConnection = new Thread(checkConnection);
                    th_checkConnection.Start();

                    
                    
                }
                catch (Exception err)
                {
                    Cleanup();
                }
            }
        }

    private void checkConnection()
    {
        bool status = true;
        while (status == true)
        {
            status = IsConnected();
            if (status == true) 
            {
                System.Threading.Thread.Sleep(3000); //Wait 3 seconds then try again
            }
            else
            {
                Cleanup();
            }
            
        }

    }
    

private bool IsConnected()
        {
            try
            {
                return !(client.Client.Poll(1, SelectMode.SelectRead) && client.Client.Available == 0);
            }
            catch (SocketException) { Cleanup(); return false; }
        }

CodePudding user response:

I did this by simply creating a forloop for each client in my client list:

private void checkConnection()
        {
            bool status = true;
            while (true)
            {
                for (int i = 0; i < clientList.Count; i  )
                {
                    Debug.WriteLine(clientList.Count);
                    status = IsConnected(i);
                    if (status == true)
                    {

                    }
                    else
                    {
                        Cleanup(i);
                    }

                }
                System.Threading.Thread.Sleep(3000); //Wait 3 seconds then try again
            }
        }

        private bool IsConnected(int i)
        {
            try
            {
                return !(clientList[i].Client.Poll(1, SelectMode.SelectRead) && clientList[i].Client.Available == 0);
            }
            catch (SocketException) { Cleanup_dep(); return false; }
        }
  • Related