Esempio n. 1
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();
        }
Esempio n. 2
0
        private void button_groupTalk_Click(object sender, EventArgs e)
        {
            int member_num = listView_friendList.SelectedItems.Count;

            if (member_num == 0)
            {
                MessageBox.Show(this, "请先选择群聊成员!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Dictionary <string, Net_class> connected_name_NC = new Dictionary <string, Net_class>();
            List <string> failed_names = new List <string>();

            for (int i = 0; i < member_num; i++)
            {
                string his_name = listView_friendList.SelectedItems[i].Text;

                string his_ip = Friends_ip[his_name];

                Net_class NC_chat_apply = new Net_class(his_ip, Net_class.client_listenGroupChat_port);

                NC_chat_apply.try_connect(3000);
                if (!NC_chat_apply.sock.Connected)
                {
                    failed_names.Add(his_name);
                    continue;
                }

                string    new_port_str = NC_chat_apply.Receive_string(); //接收对方传来的端口信息。
                Net_class NC_chat_new  = new Net_class(his_ip, int.Parse(new_port_str));
                NC_chat_new.try_connect(3000);
                if (!NC_chat_new.sock.Connected)
                {
                    failed_names.Add(his_name);
                    continue;
                }

                connected_name_NC.Add(his_name, NC_chat_new);
            }

            groupChatWindow CW = new groupChatWindow(connected_name_NC, myName);
            Thread          Thread_friendList = new Thread(() => Application.Run(CW));

            Thread_friendList.SetApartmentState(ApartmentState.STA); //要加这句,否则不能打开fileopendialog
            Thread_friendList.Start();

            if (failed_names.Count > 0)
            {
                string failed_names_str = "";
                for (int i = 0; i < failed_names.Count; i++)
                {
                    failed_names_str = failed_names_str.Insert(failed_names_str.Length, failed_names[i] + " ");
                }

                MessageBox.Show(this, "下列用户连接失败:\r\n " + failed_names_str, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 3
0
        private void button_addFriend_Click(object sender, EventArgs e)
        {
            addFriend aF = new addFriend();

            aF.ShowDialog();
            string friend_name = aF.friend_name;

            if (friend_name == "")
            {
                return;
            }

            server_NC.Send_message("q" + friend_name);//查询好友状态
            string receive_string = server_NC.Receive_string();

            if (receive_string == "n" || receive_string.StartsWith("Please"))
            {
                DialogResult dr = MessageBox.Show(this, "对方不在线,添加好友失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                Net_class NC_friend_apply = new Net_class(receive_string, Net_class.client_listenF_port);

                NC_friend_apply.try_connect(3000);
                if (!NC_friend_apply.sock.Connected)
                {
                    DialogResult dr = MessageBox.Show(this, "无法连接到对方网络,请稍后重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                /*try
                 * { NC_friend_apply.try_connect(); }
                 * catch(SocketException)
                 * {
                 *  DialogResult dr = MessageBox.Show(this, "无法连接到对方网络,请稍后重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                 *  return;
                 * }*/

                NC_friend_apply.Send_message("_f" + myName);                                                                                 //发送好友申请

                Thread thr = new Thread(() => MessageBox.Show("已发送好友申请!\r\n对方在线。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)); //显示提示窗口但不将程序挂起
                thr.IsBackground = true;
                thr.Start();

                //NC_friend_apply.Close();
            }
        }
Esempio n. 4
0
        //发起聊天
        private void listView_friendList_DoubleClick(object sender, EventArgs e)
        {
            if (listView_friendList.SelectedItems.Count == 0)
            {
                return;
            }

            string his_name = listView_friendList.SelectedItems[0].Text;

            string his_ip = Friends_ip[his_name];

            Net_class NC_chat_apply = new Net_class(his_ip, Net_class.client_listenChat_port);

            /*Thread thr = new Thread(() => MessageBox.Show("连接中。。。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)); //显示提示窗口但不将程序挂起
             * thr.IsBackground = true;
             * thr.Start();*/

            NC_chat_apply.try_connect(3000);
            if (!NC_chat_apply.sock.Connected)
            {
                DialogResult dr = MessageBox.Show(this, "连接对方网络失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            string    new_port_str = NC_chat_apply.Receive_string(); //接收对方传来的端口信息。
            Net_class NC_chat_new  = new Net_class(his_ip, int.Parse(new_port_str));

            NC_chat_new.try_connect(3000);
            if (!NC_chat_new.sock.Connected)
            {
                DialogResult dr = MessageBox.Show(this, "连接对方网络失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            NC_chat_new.Send_message("_c" + myName);

            addChatWin(his_ip, his_name, NC_chat_new);
        }
Esempio n. 5
0
        //收到聊天消息的callback
        public void clientChat_receive(IAsyncResult asy_result)
        {
            Socket    receive_socket  = (Socket)asy_result.AsyncState;
            Net_class receive_NC      = new Net_class(receive_socket);
            int       my_receive_port = receive_NC.get_my_port();

            IPEndPoint friend_endP = (IPEndPoint)receive_socket.RemoteEndPoint;
            string     friend_ip   = friend_endP.Address.ToString();

            try
            {
                int    len;
                string message = receive_NC.Receive_string(asy_result, buffer_Chat, out len);

                //聊天邀请
                if (message.Length > 2 && message.StartsWith("_c"))
                {
                    string his_name = message.Substring(2);

                    string his_ip = friend_ip;
                    //Friends_ip.TryGetValue(his_name, out his_ip);

                    if (!receive_NC.sock.Connected)
                    {
                        DialogResult dr = MessageBox.Show(this, "与用户:" + his_name + "的聊天创建失败。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    chatting_ports.Add(my_receive_port);
                    addChatWin(his_ip, his_name, receive_NC);
                }
            }
            catch (SocketException e)
            {
                MessageBox.Show(e.ToString(), "异常",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 6
0
        //收到消息
        public void clientChat_receive(IAsyncResult asy_result)
        {
            Net_class receive_NC;
            Socket    receive_socket;

            if (!Sock_connected.sock.Connected)
            {
                Sock_connected.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_Chat_receive, out receive_len);

                //聊天信息
                if (message.Length > 1 && message[0] == 1)
                {
                    message = message.Substring(1);
                    string his_name = (message.Split('_')[0]);
                    add_msg2list(his_name, message.Split('_')[1], Color.Black);
                }
                //收到文件
                else if (receive_len > 1 && buffer_Chat_receive[0] == 2)
                {
                    string file_suffix = file_name.Substring(file_name.LastIndexOf('.'));

                    DialogResult re = MessageBox.Show("对方发来文件:" + file_name + ",是否接收?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

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

                    SaveFileDialog save_file_Dialog = new SaveFileDialog()
                    {
                        Filter   = "(*" + file_suffix + ")|*" + file_suffix + "",
                        FileName = 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_Chat_receive, 1, receive_len - 1);
                            fs.Flush();
                            fs.Close();
                        }

                        add_msg2list("系统消息:", "接受到的文件已经保存:" + savePath, Color.Red);
                    }
                }
                //收到文件名
                else if (receive_len > 1 && buffer_Chat_receive[0] == 3)
                {
                    message   = message.Substring(1);
                    file_name = message;
                }

                receive_socket.BeginReceive(buffer_Chat_receive, 0, buffer_Chat_receive.Length, SocketFlags.None, new AsyncCallback(clientChat_receive), receive_socket);
            }
            catch (SocketException e)
            {
                //MessageBox.Show(e.ToString(), "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 7
0
        //收到添加好友消息的callback
        public void clientF_receive(IAsyncResult asy_result)
        {
            Socket    receive_socket = (Socket)asy_result.AsyncState;
            Net_class receive_NC     = new Net_class(receive_socket);

            IPEndPoint friend_endP = (IPEndPoint)receive_socket.RemoteEndPoint;
            string     friend_ip   = friend_endP.Address.ToString();

            try
            {
                int    len;
                string message = receive_NC.Receive_string(asy_result, buffer_F, out len);

                if (message.Length < 3)
                {
                    return;
                }

                string friend_name = message.Substring(2);

                if (message.StartsWith("_f")) //收到好友申请
                {
                    DialogResult dr = MessageBox.Show(this, "用户:" + friend_name + " 请求添加好友,是否同意?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    string       friend_reply;

                    if (dr == DialogResult.Yes) //接收好友申请
                    {
                        friend_reply = "_y";
                        add_friend2list(friend_name, friend_ip);
                    }
                    else //拒绝好友申请
                    {
                        friend_reply = "_n";
                    }

                    Net_class NC_friend_agree = new Net_class(friend_ip, Net_class.client_listenF_port);

                    NC_friend_agree.try_connect(3000);
                    if (!NC_friend_agree.sock.Connected)
                    {
                        MessageBox.Show(this, "无法连接到对方网络,请稍后重试。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    NC_friend_agree.Send_message(friend_reply + myName); //发送回复
                    if (dr == DialogResult.Yes)                          //添加到好友列表
                    {
                        add_friend2list(friend_name, friend_ip);
                    }
                }
                else if (message.StartsWith("_y")) //好友申请被接受
                {
                    DialogResult dr = MessageBox.Show(this, "用户:" + friend_name + " 已接受您的好友申请。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    add_friend2list(friend_name, friend_ip);
                }
                else if (message.StartsWith("_n"))
                {
                    DialogResult dr = MessageBox.Show(this, "用户:" + friend_name + " 已拒绝您的好友申请。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                //receive_NC.sock.BeginReceive(buffer_F, 0, buffer_F.Length, SocketFlags.None, new AsyncCallback(clientF_receive), receive_NC.sock);
            }
            catch (SocketException e)
            {
                MessageBox.Show(e.ToString(), "异常",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Esempio n. 8
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);
            }
        }