/// <summary> /// 循环接收客户端1的请求数据 /// </summary> public void AccpetClient1Data() { NetworkStream Ns1 = client1.GetStream(); string str1 = ""; while (true) { PokerGroup pg = new PokerGroup(); byte[] bytes1 = new byte[108]; Ns1.Read(bytes1, 0, 108); str1 = Encoding.Default.GetString(bytes1); if (str1.StartsWith("Name")) { str1 = str1.Replace("Name", ""); this.client1Name = str1; this.SendDataForClient("CName" + str1, 2); continue; } if (str1.StartsWith("OK")) { this.client1IsOk = true; continue; } if (str1.StartsWith("PokerCount")) { Thread.Sleep(sleep); SendDataForClient(str1, 2); str1 = str1.Replace("PokerCount", ""); int PokerCount = Convert.ToInt32(str1); if (PokerCount == 0 && DConsole.IsStart) { DConsole.Winer = 2; DConsole.Restart(); } DConsole.PaintClient(PokerCount, 1); continue; } if (str1.StartsWith("client")) { SendDataForClient(str1, 2); Thread.Sleep(sleep); str1 = str1.Replace("client", ""); pg.GetPokerGroup(Encoding.Default.GetBytes(str1)); DConsole.leadedPokerGroups.Add(pg); DConsole.PaintPlayer2LeadPoker(pg); DConsole.WriteLeadedPokers(); if (!DConsole.IsRestart) { DConsole.player1.haveOrder = true; //client1出牌后归server出牌,前提是没有Restart,Restart后出牌权限消失 } else { DConsole.IsRestart = false; } continue; } //Client放弃出牌,权限交给服务器 if (str1.StartsWith("Pass")) { DConsole.gPlayer2LeadPoker.Clear(DConsole.backColor); DConsole.gPlayer2LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 5, 5); DConsole.player1.haveOrder = true; SendDataForClient("ClientPass", 2); continue; } if (str1.StartsWith("IamIsBiggest")) { DConsole.player1.isBiggest = false; this.SendDataForClient("NoBiggest", 2); continue; } if (str1.StartsWith("AreYouLandLord")) { if (DConsole.LandLordNum == 1) { DConsole.Restart(); continue; } DConsole.player1.areYouLandLord = true; continue; } if (str1.StartsWith("IamLandLord")) { DConsole.lblClient1Name.Text += "(地主)"; DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Red; SendDataForClient("ClientIsLandLord", 2); Thread.Sleep(sleep); DConsole.PaintClient(20, 1); SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 1); Thread.Sleep(sleep); SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 2); DConsole.player1.SelectLandLordEnd(); continue; } } }
/// <summary> /// 接收服务器发送的数据 /// </summary> public void AcceptServerData() { NetworkStream Ns = client.GetStream(); string str = ""; while (true) { byte[] bytes = new byte[108]; Ns.Read(bytes, 0, 108); str = Encoding.Default.GetString(bytes); if (str.StartsWith("StartPokers")) { DConsole.IsStart = true; str = str.Replace("StartPokers", ""); str.Trim(); byte[] bytePokers = Encoding.Default.GetBytes(str); PokerGroup pokers = new PokerGroup(bytePokers); if (pokers.Count == 17) { DConsole.player1.pokers.Clear(); DConsole.player1.pokers = pokers; DConsole.player1.sort(); DConsole.player1.Paint(); } DConsole.PaintLandLord(); continue; } if (str.StartsWith("CName")) { str = str.Replace("CName", ""); DConsole.OtherClientName = str; continue; } if (str.StartsWith("SPokerCount")) { str = str.Replace("SPokerCount", ""); int pokerCount = Convert.ToInt32(str); DConsole.PaintServer(pokerCount); continue; } if (str.StartsWith("PokerCount")) { str = str.Replace("PokerCount", ""); int pokerCount = Convert.ToInt32(str); DConsole.PaintClient(pokerCount); continue; } if (str.StartsWith("YouAreClient1")) { DConsole.ChangePlace(); continue; } if (str.StartsWith("EveryOneIsOk")) { this.everyIsOk = true; continue; } if (str.StartsWith("server")) { PokerGroup pokers = new PokerGroup(); str = str.Replace("server", ""); byte[] bytePg = Encoding.Default.GetBytes(str); pokers.GetPokerGroup(bytePg); DConsole.leadedPokerGroups.Add(pokers); DConsole.WriteLeadedPokers(); DConsole.PaintPlayer2LeadPoker(pokers); continue; } if (str.StartsWith("client")) { PokerGroup pokers = new PokerGroup(); str = str.Replace("client", ""); byte[] bytePg = Encoding.Default.GetBytes(str); pokers.GetPokerGroup(bytePg); DConsole.leadedPokerGroups.Add(pokers); DConsole.WriteLeadedPokers(); DConsole.PaintPlayer3LeadPoker(pokers); continue; } if (str.StartsWith("Order")) { DConsole.player1.haveOrder = true; continue; } if (str.StartsWith("ClientPass")) { DConsole.gPlayer3LeadPoker.Clear(DConsole.backColor); DConsole.gPlayer3LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 5, 5); continue; } if (str.StartsWith("ServerPass")) { DConsole.gPlayer2LeadPoker.Clear(DConsole.backColor); DConsole.gPlayer2LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 5, 5); continue; } if (str.StartsWith("IsBiggest")) { DConsole.player1.isBiggest = true; continue; } if (str.StartsWith("NoBiggest")) { DConsole.player1.isBiggest = false; continue; } if (str.StartsWith("AreYouLandLord")) { DConsole.player1.areYouLandLord = true; continue; } if (str.StartsWith("LandLordPokers")) { PokerGroup pokers = new PokerGroup(); str = str.Replace("LandLordPokers", ""); byte[] bytePg = Encoding.Default.GetBytes(str); pokers.GetPokerGroup(bytePg); DConsole.LandLordPokers = pokers; DConsole.player1.SelectLandLordEnd(); continue; } if (str.StartsWith("ClientIsLandLord")) { DConsole.lblClient2Name.Text += "(地主)"; DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Red; DConsole.PaintClient(20); continue; } if (str.StartsWith("ServerIsLandLord")) { DConsole.lblClient1Name.Text += "(地主)"; DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Red; DConsole.PaintServer(20); continue; } if (str.StartsWith("ReStart")) { DConsole.leadedPokerGroups.Clear(); DConsole.leadPokers.Clear(); DConsole.player1.pokers.Clear(); DConsole.player1.areYouLandLord = false; DConsole.player1.isBiggest = false; DConsole.player1.isLandLord = false; DConsole.player1.haveOrder = false; DConsole.lblClient1Name.Text = DConsole.lblClient1Name.Text.Replace("(地主)", ""); DConsole.lblClient2Name.Text = DConsole.lblClient2Name.Text.Replace("(地主)", ""); DConsole.lblClient1Name.ForeColor = System.Drawing.Color.Black; DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Black; DConsole.PaintLandLord(false); DConsole.IsRestart = true; continue; } if (str.StartsWith("YouScore")) { str = str.Replace("YouScore", ""); DConsole.serverScore = Convert.ToInt32(str); continue; } if (str.StartsWith("ClientScore")) { str = str.Replace("ClientScore", ""); DConsole.client2Score = Convert.ToInt32(str); continue; } if (str.StartsWith("ServerScore")) { str = str.Replace("ServerScore", ""); DConsole.client1Score = Convert.ToInt32(str); continue; } } }
/// <summary> /// 循环接收客户端2的请求数据 /// </summary> public void AccpetClient2Data() { NetworkStream Ns2 = client2.GetStream(); string str1 = ""; while (true) { PokerGroup pg = new PokerGroup(); byte[] bytes2 = new byte[108]; Ns2.Read(bytes2, 0, 108); str1 = Encoding.Default.GetString(bytes2); if (str1.StartsWith("Name")) { str1 = str1.Replace("Name", ""); this.client2Name = str1; this.SendDataForClient("CName" + str1, 1); continue; } if (str1.StartsWith("OK")) { this.client2IsOk = true; continue; } if (str1.StartsWith("PokerCount")) { Thread.Sleep(sleep); SendDataForClient(str1, 1); str1 = str1.Replace("PokerCount", ""); int PokerCount = Convert.ToInt32(str1); if (PokerCount == 0 && DConsole.IsStart) { DConsole.Winer = 3; DConsole.Restart(); } DConsole.PaintClient(PokerCount, 2); continue; } if (str1.StartsWith("client")) { SendDataForClient(str1, 1); Thread.Sleep(sleep); str1 = str1.Replace("client", ""); pg.GetPokerGroup(Encoding.Default.GetBytes(str1)); DConsole.leadedPokerGroups.Add(pg); DConsole.PaintPlayer3LeadPoker(pg); DConsole.WriteLeadedPokers(); if (!DConsole.IsRestart) { SendDataForClient("Order", 1); //权限交给client1 前提是没有Restart } else { DConsole.IsRestart = false; //当检测到已经Restart时,复位Restart使它还原为false供下次使用 } System.Threading.Thread.Sleep(sleep); continue; } //Client2放弃出牌,权限交给Client1 if (str1.StartsWith("Pass")) { DConsole.gPlayer3LeadPoker.Clear(DConsole.backColor); DConsole.gPlayer3LeadPoker.DrawString("不要", new System.Drawing.Font("宋体", 20), System.Drawing.Brushes.Red, 5, 5); SendDataForClient("ClientPass", 1); Thread.Sleep(500); SendDataForClient("Order", 1); continue; } if (str1.StartsWith("IamIsBiggest")) { DConsole.player1.isBiggest = false; this.SendDataForClient("NoBiggest", 1); continue; } if (str1.StartsWith("AreYouLandLord")) { if (DConsole.LandLordNum == 2) { DConsole.Restart(); continue; } SendDataForClient("AreYouLandLord", 1); continue; } if (str1.StartsWith("IamLandLord")) { DConsole.lblClient2Name.Text += "(地主)"; DConsole.lblClient2Name.ForeColor = System.Drawing.Color.Red; SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 1); SendDataForClient("LandLordPokers", DConsole.LandLordPokers, 2); SendDataForClient("ClientIsLandLord", 1); DConsole.PaintClient(20, 2); DConsole.player1.SelectLandLordEnd(); continue; } } }