Пример #1
0
 public Packet(SerializationInfo info, StreamingContext context)
 {
     this.myIP      = (string)info.GetValue("IP", typeof(string));
     this.myName    = (string)info.GetValue("Name", typeof(string));
     this.myMessage = (string)info.GetValue("Message", typeof(string));
     this.myFont    = (Font)info.GetValue("Font", typeof(Font));
     this.myColor   = (Color)info.GetValue("Color", typeof(Color));
     this.myType    = (TypePacket)info.GetValue("Type", typeof(TypePacket));
     this.myPort    = (string)info.GetValue("Port", typeof(string));
 }
Пример #2
0
 public Packet(Packet packet)
 {
     this.myIP      = packet.myIP;
     this.myName    = packet.myName;
     this.myMessage = packet.myMessage;
     this.myFont    = packet.myFont;
     this.myColor   = packet.myColor;
     this.myType    = packet.myType;
     this.myPort    = packet.myPort;
 }
Пример #3
0
 public Packet(string IP, string Name, string Mess, Font font, Color color, TypePacket type, string Port)
 {
     this.myIP      = IP;
     this.myName    = Name;
     this.myMessage = Mess;
     this.myFont    = font;
     this.myColor   = color;
     this.myType    = type;
     this.myPort    = Port;
 }
Пример #4
0
 public Packet()
 {
     this.myIP      = null;
     this.myMessage = null;
     this.myName    = null;
     this.myFont    = new Font("Arial", 13);
     this.myColor   = Color.Black;
     this.myType    = TypePacket.NONE;
     this.myPort    = rd.Next(10000, 60000) + "";
 }
Пример #5
0
        byte[] Sendpacketclose(TypePacket tp)
        {
            Packet mypacket = new Packet();

            mypacket.MyType = tp;
            MemoryStream    str     = new MemoryStream();
            BinaryFormatter bformat = new BinaryFormatter();

            bformat.Serialize(str, mypacket);
            byte[] data = new byte[1024];
            data = str.ToArray();
            return(data);
        }
Пример #6
0
        byte[] SendPacket4(string FileName, TypePacket type)
        {
            Packet mypacket = new Packet();

            mypacket.MyType    = type;
            mypacket.MyMessage = FileName;
            MemoryStream    str     = new MemoryStream();
            BinaryFormatter bformat = new BinaryFormatter();

            bformat.Serialize(str, mypacket);
            byte[] data = new byte[1024];
            data = str.ToArray();
            return(data);
        }
Пример #7
0
        public void Check(byte data)
        {
            TypePacket typeP = (TypePacket)data;

            foreach (TypePacket tp in Enum.GetValues(typeof(TypePacket)))
            {
                if (tp == typeP)
                {
                    type = typeP;
                    return;
                }
            }

            type = TypePacket.Null;
        }
Пример #8
0
        public void SendName(TypePacket type)
        {
            Packet mypacket = new Packet();

            //this.myPort = mypacket.MyPort;
            mypacket.MyIP   = GetLocalIP();
            mypacket.MyName = userName;
            mypacket.MyType = type;
            MemoryStream    str     = new MemoryStream();
            BinaryFormatter bformat = new BinaryFormatter();

            bformat.Serialize(str, mypacket);
            byte[] data = new byte[1024];
            data = str.ToArray();
            sendingClient.Send(data, data.Length);
        }
Пример #9
0
 public PacketType()
 {
     type = TypePacket.Null;
 }
Пример #10
0
        // XỬ LÝ NHẬN TIN
        private void Receiver()
        {
            IPEndPoint endPoint        = new IPEndPoint(IPAddress.Any, port);
            AddMessage messageDelegate = MessageReceived;

            while (true)
            {
                byte[] data = receivingClient.Receive(ref endPoint);
                #region NHẬN FILE
                if (curType == TypePacket.SEND_FILE)
                {
                    //Loại việc nhận tin của chính mình
                    if (curIP == GetLocalIP())
                    {
                        curType = TypePacket.MESSAGE;
                    }
                    else
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            SaveFileDialog saveFileDlog1 = new SaveFileDialog();
                            saveFileDlog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                            saveFileDlog1.FileName = saveFileName;
                            saveFileName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\" + saveFileDlog1.FileName;
                        }));
                        bool       bTranferOk = true;
                        FileStream fs         = null;
                        try
                        {
                            fs = File.Create(saveFileName);
                            fs.Write(data, 0, data.Length);
                        }
                        catch (Exception e)
                        {
                            bTranferOk = false;
                            MessageBox.Show(e.Message, "Nhận file bị lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        if (fs != null)
                        {
                            try { fs.Close(); }
                            catch (Exception) { }
                        }
                        if (!bTranferOk)
                        {
                            try { File.Delete(saveFileName); }
                            catch (Exception) { }
                        }
                        else
                        {
                            string[] extension;
                            extension = saveFileName.Split('.');
                            //Hiện lên Web
                            this.Invoke((MethodInvoker)(() =>
                            {
                                Packet rpck = new Packet();
                                rpck.MyName = curSender;
                                rpck.MyMessage = "<a href='" + saveFileName.Replace(":", "(~*)") + "'>" + (('.' + extension[1] == ".jpg" || '.' + extension[1] == ".png" || extension[1] == ".PNG") ? "<img src='" + saveFileName + "' style='max-width:300px'/><br/>" : "") + "<b>" + Path.GetFileName(saveFileName) + "</b></a>";
                                Message fmess = new Message(rpck, Type.RECEIVER);
                                ChatLog.Add(fmess);
                                RefreshWeb();
                            }));
                        }
                        curType   = TypePacket.MESSAGE;
                        curIP     = "";
                        curSender = "";
                    }
                }
                #endregion

                #region TỪ CHỐI NHẬN FILE & IMAGE
                else if (curType == TypePacket.NONE)
                {
                    curType = TypePacket.MESSAGE;
                }
                #endregion

                #region NHẬN IMAGE

                else if (curType == TypePacket.SEND_IMAGE)
                {
                    if (curIP == GetLocalIP())
                    {
                        curType = TypePacket.MESSAGE;
                    }
                    else
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            SaveFileDialog saveFileDlog1 = new SaveFileDialog();
                            saveFileDlog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
                            saveFileDlog1.FileName = saveFileName;
                            saveFileName = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\" + saveFileDlog1.FileName;
                        }));
                        bool       bTranferOk = true;
                        FileStream fs         = null;
                        try
                        {
                            fs = File.Create(saveFileName);
                            fs.Write(data, 0, data.Length);
                        }
                        catch (Exception e)
                        {
                            bTranferOk = false;
                            MessageBox.Show(e.Message, "Nhận Image bị lỗi!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        if (fs != null)
                        {
                            try { fs.Close(); }
                            catch (Exception) { }
                        }
                        if (!bTranferOk)
                        {
                            try { File.Delete(saveFileName); }
                            catch (Exception) { }
                        }
                        else
                        {
                            this.Invoke((MethodInvoker)(() =>
                            {
                                Packet rpck = new Packet();
                                rpck.MyName = curSender;
                                rpck.MyMessage = "<a href='" + saveFileName.Replace(":", "(~*)") + "'>" + "<img src='" + saveFileName + "' style='max-width:300px'/><br/>" + "<b>" + Path.GetFileName(saveFileName) + "</b></a>";
                                Message fmess = new Message(rpck, Type.RECEIVER);
                                ChatLog.Add(fmess);
                                RefreshWeb();
                            }));
                        }
                        curType   = TypePacket.NONE;
                        curIP     = "";
                        curSender = "";
                    }
                }
                #endregion

                #region NHẬN MESS BÌNH THƯỜNG (XỬ LÝ PACK ĐẦU TIÊN)
                else
                {
                    Packet receivePacket = DeSerialize(data);
                    if (receivePacket.MyType == TypePacket.SEND_INFO_USER_1 && receivePacket.MyIP != meIP)
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            messageDelegate = MessageName;
                            Invoke(messageDelegate, receivePacket);
                        }));
                    }
                    else if (receivePacket.MyType == TypePacket.SEND_INFO_USER_2 && receivePacket.MyIP != meIP)
                    {
                        this.Invoke((MethodInvoker)(() =>
                        {
                            bool ck = true;
                            for (int i = 0; i < lstOnline.Count; i++)
                            {
                                string[] ck_lstOnline = lstOnline[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                if (ck_lstOnline[1] == receivePacket.MyIP)
                                {
                                    ck = false;
                                }
                            }
                            if (ck)
                            {
                                lstFriends.Items.Add(receivePacket.MyName);
                                lstOnline.Add(receivePacket.MyName + " " + receivePacket.MyIP);
                            }
                        }));
                    }
                    else if (receivePacket.MyType == TypePacket.MESSAGE)
                    {
                        if (receivePacket.MyMessage == "file#")
                        {
                            curType      = TypePacket.SEND_FILE;
                            curSender    = receivePacket.MyName;
                            curIP        = receivePacket.MyIP;
                            saveFileName = receivePacket.MyPort;
                        }
                        else if (receivePacket.MyMessage == "image#")
                        {
                            curType      = TypePacket.SEND_IMAGE;
                            curSender    = receivePacket.MyName;
                            curIP        = receivePacket.MyIP;
                            saveFileName = receivePacket.MyPort;
                        }
                        else if (receivePacket.MyMessage != "file#" && receivePacket.MyMessage != "image#")
                        {
                            messageDelegate = MessageReceived;
                            Invoke(messageDelegate, receivePacket);
                        }
                    }
                    else if (receivePacket.MyType == TypePacket.REQUIRE_CHAT)
                    {
                        messageDelegate = AcceptRequireChat;
                        Invoke(messageDelegate, receivePacket);
                    }
                    else if (receivePacket.MyType == TypePacket.OUT_CHAT)
                    {
                        messageDelegate = ClosingMethod;
                        Invoke(messageDelegate, receivePacket);
                    }
                }
                #endregion
            }
        }
Пример #11
0
 public Packet(TypePacket Type)
 {
     this.Type = Type;
 }