Example #1
0
 public static void ConnectionTearDown(ClientHandler client)
 {
     //Some client has lost connection.
     //Drop its connections at server side
     lock (activeClients)
     {
         activeClients.Remove(client);
     }
 }
Example #2
0
        static void Main(string[] args)
        {
            if (!IsInputValid(args))
            {
                Console.WriteLine("Keep going! :-)");
                return;
            }

            try
            {

                Logger.Logger.InitializeLogger(null);
                Logger.Logger.WriteInfo("Logging initialized");
                Logger.Logger.WriteInfo("Trying to listen at port: " + listningPortnumber);
                TcpListener listner = new TcpListener(new IPEndPoint(IPAddress.Any, (int)listningPortnumber));
                try
                {
                    listner.Start();

                }
                catch (Exception e)
                {
                    Console.WriteLine("Unable to listen via the provided port\n" + e.Message + "\n" + e.StackTrace);
                }

                while (true)
                {
                    if (listner.Pending())
                    {

                        TcpClient connection = listner.AcceptTcpClient();
                        Logger.Logger.WriteInfo(string.Format("Connection request from: {0}", connection.Client.RemoteEndPoint.ToString()));
                        CleanActiveClients();
                        lock (activeClients)
                        {
                            if (activeClients.Count < maxNumberOfClients)
                            {
                                Logger.Logger.WriteInfo(string.Format("Connection accepted"));
                                ClientHandler handler = new ClientHandler(connection);
                                activeClients.Add(handler);
                                handler.StartCommunication();
                            }
                            else
                            {
                                Logger.Logger.WriteInfo(string.Format("Connection will be rejected because the maximum number of connections has been reached"));
                                connection.Close();
                            }
                        }
                    }
                    listner.Start();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured in the ChatterServer application");
                Console.WriteLine("Exception details:");
                Console.WriteLine(e.Message);
                Console.WriteLine("Stack trace:");
                Console.WriteLine(e.StackTrace.ToString());
                Console.WriteLine("Try again after checking the internet connectivity");
                Console.WriteLine("Make sure that you have sufficent permission to open a port on the system");
                Console.WriteLine("Support contact: Machiry Aravind Kumar");
            }
        }