public void SendMessage(ClientHandler client, string data)
 {
     try
     {
         NetworkStream broadcastStream = client.clientSocket.GetStream();
         Byte[] broadcastBytes = Encoding.ASCII.GetBytes(data + "$");
         broadcastStream.Write(broadcastBytes, 0, broadcastBytes.Length);
         broadcastStream.Flush();
     }
     catch(Exception e)
     {
         Console.WriteLine("Was unable to send a message to {0}", client.Player.Name);
         CloseClient(client);
     }
 }
        public void Start(string ipString)
        {
            IPAddress ip = IPAddress.Parse(ipString);
            TcpListener serverSocket = new TcpListener(ip, 8888);
            TcpClient clientSocket = default(TcpClient);
            int counter = 0;

            serverSocket.Start();

            counter = 0;
            while ((true))
            {
                counter += 1;
                clientSocket = serverSocket.AcceptTcpClient();
                clientSocket.SendBufferSize = Constants.ServerToClientBufferSize;
                clientSocket.ReceiveBufferSize = Constants.ClientToServerBufferSize;

                byte[] bytesFrom = new byte[clientSocket.ReceiveBufferSize];
                string dataFromClient = null;

                NetworkStream networkStream = clientSocket.GetStream();
                networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
                dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);

                if (!dataFromClient.Contains('$'))
                {
                    Console.WriteLine("Received bad data from new joiner");
                    Console.WriteLine(dataFromClient);
                    continue;
                }
                dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));

                Player player = new Player();
                ClientHandler client = new ClientHandler();
                PlayerClientDictionary.Add(player, client);
                client.UserInputMethod = UserInputMethod;
                client.Start(player, clientSocket);

                NewUserMethod(player, dataFromClient);
            }
        }
 public void CloseClient(ClientHandler client)
 {
     try
     {
         Console.WriteLine("Closing connection to {0}", client.Player.Name);
         Player player = PlayerClientDictionary.Keys.Single(x => PlayerClientDictionary[x] == client);
         RemoveUserMethod(player);
         client.IsRunning = false;
         client.clientSocket.Close();
         PlayerClientDictionary.Remove(player);
     }
     catch(Exception e)
     {
         Console.WriteLine("Error: Couldn't remove {0}", client.Player.Name);
     }
 }