示例#1
0
        static void Main(string[] args)
        {
            serverName = args.Length > 0 ? args[0] : "Default";
            TcpListener listener = new TcpListener(IPAddress.Any, 5000);
            listener.Start();
            Console.WriteLine("Server '" + serverName +  "' up and running!");

            while (true)
            {
                //Acceptera nya klienter
                TcpClient client = listener.AcceptTcpClient(); //Kommer fastna där, står och väntar.
                NetworkStream stream = client.GetStream();
                //Få användarnamn
                byte[] recievedData = new byte[512];
                stream.Read(recievedData, 0, recievedData.Length);
                //Spara den nyuppkopplade användaren
                ChatClient user = new ChatClient { UserName = Encoding.Unicode.GetString(recievedData).TrimEnd('\0'), Client = client };
                clientList.Add(user);
                Console.WriteLine("*** User " + user.UserName + " joined. ***");
                SendMessage(user.Client, "", "Welcome to " + serverName + "! Online users: " + (clientList.Count) + ".");
                Broadcast("", user.UserName + " joined the chat.");
                user.StartListening();
            }
            //Console.ReadKey();
        }
示例#2
0
        internal static void DisconnectUser (ChatClient user)
        {
            Console.WriteLine("*** Client " + user.UserName + " disconnected. ***");
            user.Client.GetStream().Close();
            user.Client.Close();
            clientList.Remove(user);
            Broadcast("", user.UserName + " left the chat.");

            //PLACERA OM!
            //user = null;
        }