Exemplo n.º 1
0
        static public void AddUser(Socket soc)
        {
            UserList.Add(new User(soc));
            try
            {
                User UserToCheck = UserList.Single(u => u.soc == soc);
                UserToCheck.stream.Read(UserToCheck.streamCatcher, 0, 100);
                string identifier1 = Encoding.ASCII.GetString(UserToCheck.streamCatcher, 0, 1);                       //identifies wether is a function request
                string identifier2 = Encoding.ASCII.GetString(UserToCheck.streamCatcher, 1, 1);                       //identifies wether it is login or register
                if (identifier1 == "f")
                {
                    if (identifier2 == "l")
                    {
                        Login.LoginCheck(UserToCheck);
                    }
                    if (identifier2 == "r")
                    {
                        Login.RegisterUser(UserToCheck);
                    }
                }
                else
                {
                    RemoveUser(UserToCheck.soc);
                }
            }
            catch { RemoveUser(soc); }

            try
            {
                byte[]        WelcomeMessage = Encoding.ASCII.GetBytes("Connected");
                NetworkStream zda            = new NetworkStream(soc);
                zda.Write(WelcomeMessage, 0, WelcomeMessage.Length);
                zda.Write(Encoding.ASCII.GetBytes(StringOfAllOnlineUsers()), 0, StringOfAllOnlineUsers().Length);
                zda.Flush();
                zda.Close();
            }
            catch { }

            try
            {
                User currentUser = UserList.Single(u => u.soc == soc);
                //notify users
                byte[] connectMessage = Encoding.ASCII.GetBytes("fx" + Encoding.ASCII.GetString(currentUser.userName));
                ChatFunctions.WriteToAll(connectMessage);

                currentUser.stream.BeginRead(currentUser.streamCatcher, 0, 1024, ChatFunctions.Reader, currentUser.soc);
            }
            catch { }
        }
Exemplo n.º 2
0
        static public void IsUserAlive(Socket socket)
        {
            bool bool1 = socket.Poll(1000, SelectMode.SelectRead);
            bool bool2 = (socket.Available == 0);

            if ((bool1 && bool2)) //|| !socket.Connected)
            {
                User UserToRemove = UserList.Single(u => u.soc == socket);
                Console.WriteLine("User {0} disconnected!", Encoding.ASCII.GetString(UserToRemove.userName));
                RemoveUser(socket);

                //notify users
                byte[] disconnectMessage = Encoding.ASCII.GetBytes("fz" + Encoding.ASCII.GetString(UserToRemove.userName));
                ChatFunctions.WriteToAll(disconnectMessage);
            }
        }