예제 #1
0
        private static void HandleClient(object obj)
        {
            var client = (TcpClient)obj;

            lock (typeof(Server))
            {
                ConnectedUsers++;
            }

            try
            {
                IPAddress ip = IPAddress.Parse(((
                                                    IPEndPoint)client.Client.RemoteEndPoint).Address.ToString());
                var writer = new StreamWriter(client.GetStream(), Encoding.ASCII);
                var reader = new StreamReader(client.GetStream(), Encoding.ASCII);
                BackendScreen.WriteHint(ip + " connected.");
                try
                {
                    var ws = new WelcomeScreen(reader, writer);
                    ws.Start();
                }
                catch (IOException)
                {
                    BackendScreen.WriteHint(ip + " disconnected.");
                }
                catch (Exception e)
                {
                    BackendScreen.WriteError("Unhandled " + e.GetType().Name + " occured while executing the UI: " +
                                             e.Message);
                }
                finally
                {
                    lock (typeof(Server))
                    {
                        ConnectedUsers--;
                    }
                    BackendScreen.WriteHint(ip + " disconnected.");
                }

                client.Close();
            }
            catch (Exception e)
            {
                BackendScreen.WriteError("Unhandled " + e.GetType().Name +
                                         " occured while executing handling connection: " + e.Message);
            }
        }