Пример #1
0
        private void button_send_Click(object sender, EventArgs e)
        {
            try
            {
                string send_msg = richTextBox_send.Text;
                //如果当前写字框没有被占用
                while (richTextBox_show_writing)
                {
                }
                ;
                //等到其他线程解除了写字框的占用
                richTextBox_show_writing = true;   //占用
                RichBox_Show rb_s        = new RichBox_Show(ShowMsg_inRichTextBox);
                string       show_string = DateTime.Now.ToString()
                                           + "\n我说:\n" + send_msg + "\n\n";
                this.Invoke(rb_s, new object[] { show_string, Color.Blue, HorizontalAlignment.Right });
                richTextBox_show_writing = false;  //恢复不被占用
                richTextBox_send.Text    = "";
                send_msg = DateTime.Now.ToString() + "\n" + my_IDnumber + "说:\n" + send_msg + "\n\n";

                foreach (Socket Client in alllinks)
                {
                    if (Client == null)
                    {
                        break;
                    }
                    AsynSend(Client, send_msg);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("好友已关闭会话,不能发送信息", "出错啦。。。",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Пример #2
0
        /// <summary>
        /// 异步接受客户端消息
        /// </summary>
        /// <param name="tcpClient"></param>
        public void AsynRecive(Socket[] links)
        {
            byte[] datarec = new byte[1024];
            try
            {
                foreach (Socket tcpClient in links)   //遍历所有连接的套接字
                {
                    if (tcpClient == null)
                    {
                        break;
                    }
                    tcpClient.BeginReceive(datarec, 0, datarec.Length, SocketFlags.None,
                                           asyncResult =>
                    {
                        int length = 0;
                        try
                        {
                            length         = tcpClient.EndReceive(asyncResult);
                            string rcv_msg = Encoding.UTF8.GetString(datarec, 0, length);

                            if (length == 0)
                            {
                                MessageBox.Show("好友退出了聊天", "信息提示",
                                                MessageBoxButtons.OK, MessageBoxIcon.Information);
                                this.Close();
                                return;
                            }
                            //如果是服务器,则向其他客户端转发该消息
                            foreach (Socket otherClient in links)
                            {
                                if (otherClient == null)
                                {
                                    break;
                                }
                                if (otherClient != tcpClient)
                                {
                                    AsynSend(otherClient, rcv_msg);
                                }
                            }

                            if (rcv_msg == "<__cmd__transfer__myfile__>")
                            {
                                allDone.Reset();
                                receive_save r_s = new receive_save(ReceiveFileConnect);
                                this.Invoke(r_s, new object[] { tcpClient });
                                allDone.WaitOne();
                            }

                            else
                            {
                                //如果当前写字框没有被占用
                                while (richTextBox_show_writing)
                                {
                                }
                                ;
                                //等到其他线程解除了写字框的占用
                                richTextBox_show_writing = true;   //占用之
                                RichBox_Show rb_s        = new RichBox_Show(ShowMsg_inRichTextBox);
                                string show_string       = rcv_msg;
                                this.Invoke(rb_s, new object[] { show_string, Color.Black, HorizontalAlignment.Left });
                                richTextBox_show_writing = false;  //恢复不被占用
                            }
                            AsynRecive(links);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(this, ex.ToString(), "出现异常",
                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }, null);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.ToString(), "出现异常",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }
        }