Example #1
0
        public void sendToAll(millClass newMill)
        {
            string message = "A Millionaire named " + newMill.Name + " has joined the boat.The richest person on the boat right now is " + richest.Name;

            lock (millDic)
            {
                foreach (millClass mill in millDic.Values)
                {
                    mill.socket.Send(Encoding.ASCII.GetBytes(message));
                }
            }
        }
Example #2
0
        public void sendToAll2(millClass newMill)
        {
            string message = newMill.Name + " has updated his/her income.The richest person on the boat right now is " + richest.Name;

            lock (millDic)
            {
                foreach (millClass mill in millDic.Values)
                {
                    mill.socket.Send(Encoding.ASCII.GetBytes(message));
                }
            }
        }
Example #3
0
        public static void checkRich()
        {
            int max = -1;

            lock (millDic)
            {
                foreach (millClass m in millDic.Values)
                {
                    if (m.Money > max)
                    {
                        richest = m;
                        max     = m.Money;
                    }
                }
            }
        }
Example #4
0
        public void handleClient(Socket socket)
        {
            byte[] msg = Encoding.ASCII.GetBytes("Wellcome to the " + name + "! What is your name?");
            socket.Send(msg);
            byte[] answer = new byte[256];
            socket.Receive(answer);
            String    millName = getData(Encoding.ASCII.GetString(answer));
            String    ip       = ((IPEndPoint)socket.RemoteEndPoint).Address + ":" + ((IPEndPoint)socket.RemoteEndPoint).Port;
            millClass temp     = new millClass(socket, millName, 0, Thread.CurrentThread);

            lock (millDic) {
                millDic.Add(ip, temp);
            }
            checkRich();
            sendToAll(temp);
            try
            {
                while (stop)
                {
                    byte[] conversation = new byte[256];
                    socket.Receive(conversation);

                    if (Encoding.ASCII.GetString(conversation)[0] == '\r' && Encoding.ASCII.GetString(conversation)[0] == '\n')
                    {
                        break;
                    }
                    if (Int32.TryParse(Encoding.ASCII.GetString(conversation), out int money))
                    {
                        temp.Money = money;
                        checkRich();
                        sendToAll2(temp);
                    }
                }
                lock (millDic)
                {
                    Thread tmp = millDic[ip].t;
                    millDic.Remove(ip);
                    checkRich();
                    tmp.Abort();
                }
                socket.Close();
            }
            catch (SocketException e)//boat throw all the millionaires
            {
            }
        }