示例#1
0
        void Connect()
        {
            dsMayKhach = new List <MayKhach>();
            ip         = new IPEndPoint(IPAddress.Any, Port);
            Sever      = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
            Sever.Bind(ip);
            Thread listen = new Thread(() =>
            {
                try
                {
                    while (true)
                    {
                        Sever.Listen(100);
                        var client   = Sever.Accept();
                        var mayKhach = new MayKhach(client);



                        Thread receive       = new Thread(NhanTinVe);
                        receive.IsBackground = true;
                        receive.Start(mayKhach);
                    }
                }
                catch
                {
                    ip    = new IPEndPoint(IPAddress.Any, Port);
                    Sever = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
                }
            });

            listen.IsBackground = true;
            listen.Start();
        }
示例#2
0
        private void PhanLuongCauChat(string chuoi, MayKhach mayNhan)
        {
            var tenMayKhach = LayTenMayKhachTrenServer(chuoi);

            if (mayNhan.TenMay == null)
            {
                if (KiemTraTenMayTrenServer(tenMayKhach))
                {
                    dsMayKhach.Add(mayNhan);
                    mayNhan.TenMay = tenMayKhach;
                    mayNhan.SocKet.Send(XuLuTinDi("[]"));
                }
                else if (dsMayKhach.Count == 1)
                {
                    dsMayKhach[0].SocKet.Send(XuLuTinDi("[[]"));
                }
                else
                {
                    mayNhan.SocKet.Send(XuLuTinDi("[]]"));
                    TatMayNhanTin(mayNhan);
                }
            }
            else if (KiemTraTenMayNhan(tenMayKhach))
            {
                var cauGui       = LayCauChatDen(chuoi, tenMayKhach);
                var DsTenMayNhan = tenMayKhach.Remove(0, 1);
                DsTenMayNhan = DsTenMayNhan.Remove(DsTenMayNhan.Length - 1, 1);
                if (DsTenMayNhan == "/*")
                {
                    ChuyenTin(cauGui, dsMayKhach, mayNhan.TenMay);
                    return;
                }
                var ten      = DsTenMayNhan.Split('&');
                var nhomChat = new List <MayKhach>();
                nhomChat.Add(mayNhan);
                foreach (var item in ten)
                {
                    var tenMayGui = LayMayKhachTuongUng(item);
                    if (tenMayGui != null)
                    {
                        nhomChat.Add(tenMayGui);
                    }
                    else
                    {
                        mayNhan.SocKet.Send(XuLuTinDi("**/" + item));
                    }
                }
                ChuyenTin(cauGui, nhomChat, mayNhan.TenMay);
            }
            else
            {
                TatMayNhanTin(mayNhan);
                throw new Exception("lỗi");
            }
        }
示例#3
0
 void TatMayNhanTin(MayKhach mayNhan)
 {
     dsMayKhach.Remove(mayNhan);
     mayNhan.SocKet.Close();
 }