コード例 #1
0
        private void MsgType(string msgIn)
        {
            String[] splitMsgIn = msgIn.Split('-');
            switch (splitMsgIn[0])
            {
            case "Message":    //normal message, just add it to the chat box
            {
                if (!splitMsgIn[1].Equals(userNameTB.Text))
                {
                    System.DateTime today = System.DateTime.Now;
                    String          time  = today.ToString();
                    ;
                    chatTxtBx1.AppendText(time + " -> " + msgIn + Environment.NewLine);
                }
                break;
            }

            case "HELLO":     // check if new user entered the chat > found in online user box <" HELLO - myName - myIP - myPort ">
            {
                if (!onlineUsersList.Items.Contains(splitMsgIn[1]) && userNameTB.Text != splitMsgIn[1])
                {
                    onlineUsersList.Items.Add(splitMsgIn[1]);
                    ///user constructorneed to be  rebuild
                    User newUser = new User();
                    newUser.userName = splitMsgIn[1]; newUser.ip = splitMsgIn[2]; newUser.port = splitMsgIn[3];
                    listUsers.Add(newUser);
                }
                break;
            }

            case "BYE":     // remove a user from the list - recieved when someone leaves the chat
            {
                chatTxtBx1.AppendText(msgIn + Environment.NewLine);
                if (onlineUsersList.Items.Contains(splitMsgIn[1]))
                {
                    onlineUsersList.Items.Remove(splitMsgIn[1]);
                    break;
                }
                break;
            }

            case "SENDFILE":     //in case of user want to send other user a file - asking to send
            {
                string[] filename = splitMsgIn[4].Split('\\');
                string   thename  = filename[filename.Length - 1];
                var      result   = MessageBox.Show(splitMsgIn[3] + " sent this file to you:  " + thename + "do you approve", "Received file", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                switch (result)
                {
                case DialogResult.OK:
                {
                    string hmacfile = splitMsgIn[splitMsgIn.Length - 1];
                    if (msgIn.Contains(" - HMAC doesn't match"))
                    {
                        hmacfile = splitMsgIn[splitMsgIn.Length - 2];
                        msgIn    = msgIn.Remove(msgIn.IndexOf(" - HMAC doesn't match"), " - HMAC doesn't match".Length);
                    }
                    SaveFileDialog sf = new SaveFileDialog();
                    sf.FileName = thename;
                    if (sf.ShowDialog() == DialogResult.OK)
                    {
                        string savePath = Path.GetDirectoryName(sf.FileName);

                        ArrayList  n    = new ArrayList();
                        IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(splitMsgIn[1]), Int32.Parse(splitMsgIn[2]));
                        n.Add(ipep);

                        Random     rnd      = new Random();
                        int        rndPort  = rnd.Next(5500, 6000);
                        IPEndPoint listenTo = new IPEndPoint(IPAddress.Parse(tbMyIp.Text), rndPort);
                        var        listener = new TcpListener(listenTo);
                        listener.Start();

                        new Thread(() =>
                                {
                                    FileRecieve.recieve(thename, listener, savePath, hmacfile);
                                }).Start();

                        userToRecieve = "OK-" + tbMyIp.Text + "-" + nubPort.Value + "-" + userNameTB.Text + "-" + splitMsgIn[4] + "-" + rndPort;
                        send(userToRecieve, n);
                    }
                    break;
                }

                case DialogResult.Cancel:
                {
                    ArrayList  n    = new ArrayList();
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(splitMsgIn[1]), Int32.Parse(splitMsgIn[2]));
                    n.Add(ipep);
                    userToRecieve = "NO-" + tbMyIp.Text + "-" + nubPort.Value + "-" + userNameTB.Text + "-" + splitMsgIn[4];
                    send(userToRecieve, n);
                    break;
                }

                default:
                {
                    ArrayList  n    = new ArrayList();
                    IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(splitMsgIn[1]), Int32.Parse(splitMsgIn[2]));
                    n.Add(ipep);
                    userToRecieve = "NO-" + tbMyIp.Text + "-" + nubPort.Value + "-" + userNameTB.Text + "-" + openFileDialog2.FileName;
                    send(userToRecieve, n);
                    break;
                }
                }
                break;
            }

            case "OK":
            {
                try
                {
                    AutoClosingMessageBox.Show(splitMsgIn[3] + " approved to recieve the file  " + splitMsgIn[4], "Received file", 2000);
                    IPEndPoint ipEnd = new IPEndPoint(IPAddress.Parse(splitMsgIn[1]), Int32.Parse(splitMsgIn[5]));
                    new Thread(() =>
                        {
                            FileSend.send(ipEnd, splitMsgIn[4], splitMsgIn[5]);
                        }).Start();
                }
                catch (Exception ex) { AutoClosingMessageBox.Show(ex.Message, "ERROR", 150000); }

                break;
            }

            case "NO":
            {
                AutoClosingMessageBox.Show("your friend refused the file transfare ", "do not Received file", 2000);
                break;
            }
            }
        }