Пример #1
0
        public void Receive()
        {
            int nbyte = 0;

            while (true)
            {
                try
                {
                    nbyte = sockclient.Receive(bts);
                }
                catch (SocketException)
                {
                    Console.WriteLine("Клиент " + name + " Отключился");
                    baseList.cls.Remove(baseList.Search4User(name));
                    if (baseList.cls.Count != 0)
                    {
                        p_UserList userList = new p_UserList(baseList.cls);
                        byte[]     bts1     = userList.MakePocket();
                        foreach (ClClass i in baseList.cls)
                        {
                            i.sockclient.Send(bts1);
                        }
                    }
                    thR.Abort();
                }

                p_Message rMessage = new p_Message(bts);
                if (rMessage.receiver != null)
                {
                    if (rMessage.receiver == "All")
                    {
                        foreach (var i in baseList.cls)
                        {
                            if (i.name != name)
                            {
                                p_Message sMessage = new p_Message("(All) " + name, rMessage.message);
                                i.sockclient.Send(sMessage.MakePocket());
                            }
                        }
                    }
                    else
                    {
                        ClClass receiver = baseList.Search4User(rMessage.receiver);
                        if (receiver != null)
                        {
                            Send(receiver, rMessage.message);
                        }
                        else
                        {
                            Console.WriteLine("Не найден адресат");
                            continue;
                        }
                    }
                    Thread.Sleep(10);
                }
            }
        }
Пример #2
0
        private void Сonnect()
        {
            while (true)
            {
                int nbyte = 0;



                sockserver.Listen(1);
                Socket sockclient = sockserver.Accept();
                nbyte = sockclient.Receive(bts);

                if (nbyte != 0)
                {
                    p_Identificator id = new p_Identificator(bts);

                    ClClass user = Search4User(id.name);

                    if (user != null)
                    {
                        if (user.password == id.password)
                        {
                            continue;
                        }
                    }
                    else
                    {
                        ClClass client = new ClClass(sockclient, id.name, id.password);
                        cls.Add(client);

                        if (cls.Count != 0)
                        {
                            p_UserList userList = new p_UserList(cls);
                            byte[]     bts1     = userList.MakePocket();
                            foreach (ClClass i in cls)
                            {
                                i.sockclient.Send(bts1);
                            }
                        }
                        Console.WriteLine("Подключился " + id.name);
                    }
                }
            }
        }