Пример #1
0
        //群聊监听的callback
        public void clientGroupChat_accepted(IAsyncResult asy_result)
        {
            Socket listen_socket  = (Socket)asy_result.AsyncState;
            Socket receive_socket = listen_socket.EndAccept(asy_result);

            Net_class receive_net     = new Net_class(receive_socket);
            int       my_receive_port = receive_net.get_my_port();

            if (my_receive_port == Net_class.client_listenGroupChat_port) //在群聊的监听端口接收到请求,则给对方返回一个本地可用的端口后并监听该端口号
            {
                groupChat_receive_port += 1;
                while (Net_class.portInUse(groupChat_receive_port)) //判断端口chat_receive_port是否被占用,如果是则加1
                {
                    groupChat_receive_port += 1;
                }
                receive_net.Send_message_asy(groupChat_receive_port.ToString()); //向对方发送可用端口号

                string    myIP = Net_class.getMy_ip();
                Net_class listenGroupChat_custom_NC = new Net_class();
                listenGroupChat_custom_NC.bind_ip_port(myIP, groupChat_receive_port);
                listenGroupChat_custom_NC.sock.Listen(200);
                listenGroupChat_custom_NC.sock.BeginAccept(new AsyncCallback(clientGroupChat_accepted), listenGroupChat_custom_NC.sock);
            }
            else if (group_chatting_ports.FindIndex(x => x == my_receive_port) == -1) //不是正在聊天的窗口所发来的连接
            {
                groupChatWindow CW = new groupChatWindow(receive_net, myName);
                Thread          Thread_friendList = new Thread(() => Application.Run(CW));
                Thread_friendList.SetApartmentState(ApartmentState.STA); //要加这句,否则不能打开fileopendialog
                Thread_friendList.Start();
                group_chatting_ports.Add(my_receive_port);
            }

            listen_socket.BeginAccept(new AsyncCallback(clientGroupChat_accepted), listen_socket);
        }
Пример #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);
            }
        }