示例#1
0
        //監聽 Server 訊息 (Listening to the Server)
        private void Listen()
        {
            EndPoint ServerEP = (EndPoint)T.RemoteEndPoint; //Server 的 EndPoint

            byte[] B     = new byte[1023];                  //接收用的 Byte 陣列
            int    inLen = 0;                               //接收的位元組數目
            string Msg;                                     //接收到的完整訊息
            string St;                                      //命令碼
            string Str;                                     //訊息內容(不含命令碼)

            while (true)                                    //無限次監聽迴圈
            {
                try
                {
                    inLen = T.ReceiveFrom(B, ref ServerEP);//收聽資訊並取得位元組數
                }
                catch (Exception)
                {
                    T.Close();                                                                          //關閉通訊器
                    listBox1.Items.Clear();                                                             //清除線上名單
                    MessageBox.Show("伺服器斷線了!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); //顯示斷線
                    button1.Enabled = true;                                                             //連線按鍵恢復可用
                    Th.Abort();                                                                         //刪除執行緒
                }

                Msg = Encoding.Default.GetString(B, 0, inLen); //解讀完整訊息
                St  = Msg.Substring(0, 1);                     //取出命令碼 (第一個字)
                Str = Msg.Substring(1);                        //取出命令碼之後的訊息
                switch (St)                                    //依命令碼執行功能
                {
                case "L":                                      //接收線上名單
                    listBox1.Items.Clear();                    //清除名單
                    string[] M = Str.Split(',');               //拆解名單成陣列
                    player = M;
                    for (int i = 0; i < M.Length; i++)
                    {
                        listBox1.Items.Add(M[i]);                                    //逐一加入名單
                    }
                    break;

                //接收廣播訊息
                case "1":
                    TextBox5.AppendText("(公開)" + Str + "\r\n");
                    break;

                case "2":

                    if (Ans.Equals(Str))
                    {
                        TextBox7.AppendText(User + ": Hit" + "\r\n");     //顯示訊息並換行
                        TextBox8.ReadOnly    = true;
                        metroButton2.Enabled = false;
                    }
                    else
                    {
                        TextBox7.AppendText(User + ":" + Str + "\r\n");     //顯示訊息並換行
                    }
                    break;

                case "3":                                       //接收私密訊息
                    TextBox5.AppendText("(私密)" + Str + "\r\n"); //顯示訊息並換行游標移到最後
                    break;

                case "5":
                    PointShape(Str);
                    break;

                case "A":    //答案
                    Ans = Str;
                    metroLabel3.Text = Ans;
                    break;

                case "C":    //cls
                    D.Shapes.Clear();
                    break;
                }
            }
        }
示例#2
0
        private void Listen()
        {
            EndPoint ServerEP = (EndPoint)T.RemoteEndPoint;

            byte[] B     = new byte[1023];
            int    inLen = 0; //接收的位元組數目

            while (true)
            {
                try
                {
                    inLen = T.ReceiveFrom(B, ref ServerEP);//收聽資訊並取得位元組數
                }
                catch (Exception)
                {
                    T.Close();
                    listBox1.Items.Clear();                                                             //清除線上名單
                    MessageBox.Show("伺服器斷線了!", "注意", MessageBoxButtons.OK, MessageBoxIcon.Information); //顯示斷線
                    TextBox5.AppendText("(系統) : 伺服器已斷線了! \r\n");
                    button1.Enabled = true;                                                             //連線按鍵恢復可用
                    Th.Abort();                                                                         //刪除執行緒
                }

                string Msg = Encoding.Default.GetString(B, 0, inLen); //解讀完整訊息
                string St  = Msg.Substring(0, 1);                     //取出命令碼 (第一個字)
                string Str = Msg.Substring(1);                        //取出命令碼之後的訊息
                switch (St)                                           //依命令碼執行功能
                {
                case "L":                                             //接收線上名單
                    listBox1.Items.Clear();                           //清除名單
                    string[] M = Str.Split(',');                      //拆解名單成陣列
                    player = M;
                    for (int i = 0; i < M.Length; i++)
                    {
                        listBox1.Items.Add(M[i]);                                    //逐一加入名單
                    }
                    break;

                case "S":
                    Count--;
                    TextBox9.AppendText(Str + "\r\n");
                    TextBox9.AppendText(DMaster + ": +10" + "\r\n");
                    if (DMaster == User)
                    {
                        user_scro += 10;
                    }
                    break;

                case "D": TextBox5.AppendText("(公開)" + Str + "\r\n"); break;   //聊天室

                case "3": TextBox5.AppendText("(私密)" + Str + "\r\n"); break;   //私密訊息

                case "M": TextBox7.AppendText(Str + "\r\n"); break;

                case "P": PointShape(Str); break;

                case "A": Ans = Str; break;

                case "C": D.Shapes.Clear(); break;

                case "G": Game(); break;

                case "R": metroButton3.Show(); break;
                }
            }
        }