Ejemplo n.º 1
0
        public chatWindow(string in_his_ip, string in_my_ip, string in_his_name, string in_my_name, Net_class sock)
        {
            InitializeComponent();
            FormClosing += new FormClosingEventHandler(chatWindow_FormClosing); //注册窗口关闭事件

            listView_message.Columns[0].Width     = 100;
            listView_message.Columns[0].TextAlign = HorizontalAlignment.Center;
            listView_message.Columns[1].Width     = listView_message.Width - listView_message.Columns[0].Width;

            his_port = sock.get_his_port();
            my_port  = sock.get_my_port();
            his_ip   = in_his_ip;
            my_ip    = in_my_ip;
            his_name = in_his_name;
            my_name  = in_my_name;

            this.Text = "聊天:" + his_name;

            sock.Close();

            receive_sock = new UdpClient(my_port);
            send_sock    = new UdpClient();

            CheckForIllegalCrossThreadCalls = false;
            receive_tr = new Thread(new ThreadStart(clientChat_receive));
            receive_tr.IsBackground = true;
            receive_tr.Start();
        }
Ejemplo n.º 2
0
        private void button_login_Click(object sender, EventArgs e)
        {
            Net_class NC = new Net_class(textBox_serverIP.Text, int.Parse(textBox_serverPort.Text)); //自定义类,处理网络事件

            NC.try_connect(6000);
            if (!NC.sock.Connected)
            {
                DialogResult dr = MessageBox.Show(this, "无法连接到服务器,请检查服务器信息。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                NC.Close();
                return;
            }

            string user_name = textBox_userName.Text;

            NC.Send_message(user_name + "_" + textBox_password.Text);//发送用户名密码

            string receive_string = NC.Receive_string();

            if (receive_string == "lol")
            {
                DialogResult dr = MessageBox.Show(this, "登录成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                DialogResult dr = MessageBox.Show(this, "登录失败,请检查用户名密码。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Thread Thread_friendList = new Thread(() => Application.Run(new FriendList(user_name, NC)));

            Thread_friendList.Start();
            this.Close();
        }
Ejemplo n.º 3
0
        //发送消息
        private void button_send_Click(object sender, EventArgs e)
        {
            if (!Sock_connected.sock.Connected)
            {
                MessageBox.Show("对方已退出聊天窗口,聊天结束。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Sock_connected.Close();
                Close();
                return;
            }

            string text_send = textBox_message.Text;

            //Sock_connected.Send_message_asy("_m"+my_name+"_"+text_send);
            Sock_connected.Send_message_NumString_asy(1, my_name + "_" + text_send);

            add_msg2list("我", text_send, Color.Green);
            textBox_message.Text = "";//清空消息输入框
        }
Ejemplo n.º 4
0
        private void friendList_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            try
            {
                server_NC.Send_message("logout" + myName);
            }
            catch
            { }

            server_NC.Close();
        }
Ejemplo n.º 5
0
        //成员接收到信息的callback
        public void member_receive(IAsyncResult asy_result)
        {
            Net_class receive_NC;
            Socket    receive_socket;

            if (!host_connectNC.sock.Connected)
            {
                host_connectNC.Close();
                Close();
                return;
            }

            try
            {
                receive_socket = (Socket)asy_result.AsyncState;
                receive_NC     = new Net_class(receive_socket);

                IPEndPoint friend_endP = (IPEndPoint)receive_socket.RemoteEndPoint;
                string     friend_ip   = friend_endP.Address.ToString();
            }
            catch
            {
                return;
            }

            try
            {
                int    receive_len;
                string message = receive_NC.Receive_string(asy_result, buffer_member_receive, out receive_len);

                //聊天信息
                if (receive_len > 1 && message[0] == 0)
                {
                    parse_string_msg(buffer_member_receive, receive_len, out string his_name, out string msg);
                    add_msg2list(his_name, msg, Color.Black);
                }
                //收到成员名
                else if (receive_len > 1 && buffer_member_receive[0] == 1)
                {
                    listView_members.Items.Clear();//首先清空列表

                    int    name_num  = (receive_len - 1) / name_len;
                    string host_name = message.Substring(1, name_len);
                    addMember2list("群主", host_name, Color.Black);
                    for (int i = 1; i < name_num; i++)
                    {
                        string his_name = message.Substring(1 + name_len * i, name_len);
                        Color  c        = his_name == my_name ? Color.Green : Color.Black;
                        addMember2list("成员", his_name, c);
                    }
                }
                //收到文件
                else if (receive_len > 1 && buffer_member_receive[0] == 2)
                {
                    string file_suffix = applying_file.name.Substring(applying_file.name.LastIndexOf('.'));

                    DialogResult re = MessageBox.Show("文件已收到:" + applying_file.name + ",是否接收?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                    if (re == DialogResult.No)
                    {
                        return;
                    }

                    SaveFileDialog save_file_Dialog = new SaveFileDialog()
                    {
                        Filter   = "(*" + file_suffix + ")|*" + file_suffix + "",
                        FileName = applying_file.name
                    };

                    string savePath;
                    if (save_file_Dialog.ShowDialog(this) == DialogResult.OK)
                    {
                        savePath = save_file_Dialog.FileName;

                        using (FileStream fs = new FileStream(savePath, FileMode.Append, FileAccess.Write))
                        {
                            fs.Write(buffer_member_receive, 1, receive_len - 1);
                            fs.Flush();
                            fs.Close();
                        }

                        add_msg2list("系统消息:", "接受到的文件已经保存:" + savePath, Color.Red);
                    }
                }
                //收到文件信息
                else if (receive_len > 1 && buffer_member_receive[0] == 3)
                {
                    if (parse_file_msg(buffer_member_receive, receive_len, out string his_name, out myFileInfo file_info))
                    {
                        add_file2list(file_info, his_name, Color.Black);
                    }
                }
                //群组解散
                else if (receive_len > 1 && buffer_member_receive[0] == 4)
                {
                    string msg = Encoding.UTF8.GetString(buffer_member_receive, 1, receive_len - 1);
                    if (msg == "q")
                    {
                        MessageBox.Show("群组已解散!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                        client_activeClose = false;
                        host_connectNC.Close();
                        this.Close();
                        return;
                    }
                }
                //没有找到文件
                if (receive_len > 1 && message[0] == 5)
                {
                    add_msg2list("系统消息:", "没有找到文件:" + applying_file.name, Color.Red);
                }

                receive_socket.BeginReceive(buffer_member_receive, 0, buffer_member_receive.Length, SocketFlags.None, new AsyncCallback(member_receive), receive_socket);
            }
            catch (SocketException e)
            {
                //MessageBox.Show(e.ToString(), "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }