예제 #1
0
        //聊天监听的callback
        public void clientChat_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_listenChat_port) //在聊天的监听端口接收到请求,则给对方返回一个本地可用的端口后并监听该端口号
            {
                chat_receive_port += 1;
                while (Net_class.portInUse(chat_receive_port)) //判断端口chat_receive_port是否被占用,如果是则加1
                {
                    chat_receive_port += 1;
                }
                receive_net.Send_message_asy(chat_receive_port.ToString()); //向对方发送可用端口号

                string    myIP = Net_class.getMy_ip();
                Net_class listenChat_custom_NC = new Net_class();
                listenChat_custom_NC.bind_ip_port(myIP, chat_receive_port);
                listenChat_custom_NC.sock.Listen(200);
                listenChat_custom_NC.sock.BeginAccept(new AsyncCallback(clientChat_accepted), listenChat_custom_NC.sock);
            }
            else if (chatting_ports.FindIndex(x => x == my_receive_port) == -1) //不是正在聊天的窗口所发来的连接
            {
                //开启接收消息
                receive_socket.BeginReceive(buffer_Chat, 0, buffer_Chat.Length, SocketFlags.None, new AsyncCallback(clientChat_receive), receive_socket);
            }

            //开始接受下一个聊天请求
            listen_socket.BeginAccept(new AsyncCallback(clientChat_accepted), listen_socket);
        }
예제 #2
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();
        }
예제 #3
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);
        }
예제 #4
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);
            }
        }