void ReceivedByClient() { Socket socketReceive = CreateSocketClient.ReceiveSocket(); while (true) { Socket temp = null; try { temp = socketReceive.Accept(); byte[] messageReceivedByServer = new byte[100]; temp.Receive(messageReceivedByServer, SocketFlags.None); string str = Encoding.ASCII.GetString(messageReceivedByServer); //identify player 1 if (str.Contains("Wait for the other player")) { GlobalClient.player1 = true; } string msg = ExtractMessage.PlayerId(str); //start timer when receiving game start message if (msg.Contains("Game Start!")) { socketReceive.Close(); newTimer.Enabled = true; newTimer.Elapsed += new System.Timers.ElapsedEventHandler(send); newTimer.Interval = 1000; break; } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace + "\n" + ex.HelpLink + "\n" + ex.InnerException + "\n" + ex.Source + "\n" + ex.TargetSite); } temp.Close(); } }
public void ReceivedByClient() { if (control == 1) { control = 0; } Socket socketReceive = CreateSocketClient.ReceiveSocket(); string msg = "??????????"; string temporaryString = ""; char[] ch = msg.ToCharArray(); while (control == 0) { Socket temp = null; Player1ScoreLabel.Text = GlobalClient.player1score; Player2ScoreLabel.Text = GlobalClient.player2score; try { if (control == 0) { temp = socketReceive.Accept(); byte[] messageReceivedByServer = new byte[100]; int sizeOfReceivedMessage = temp.Receive(messageReceivedByServer, SocketFlags.None); string str = Encoding.ASCII.GetString(messageReceivedByServer); int endPos = str.IndexOf("#"); //Enable buttons when receive !play! if (str.Contains("!play!")) { Consonant.Enabled = true; Vowel.Enabled = true; } //Disable buttons when receive !wait! else if (str.Contains("!wait!")) { Consonant.Enabled = false; Vowel.Enabled = false; } //Set Players score in global variable if (str.Contains("$")) { btnNewRound.Enabled = true; ExtractMessage.SetScores(str, endPos); } //Inform player that word does not exist else if (str.Contains("!Invalid!")) { btnNewRound.Enabled = false; string message = "Word does not exist!! You have been awarded 0 Marks"; DialogResult d = MessageBox.Show(message); } //Play continues until receive !Reset! else if (str.Contains("!Reset!")) { control = 1; } else if (str.Contains("!DisplayScore!")) { socketReceive.Close(); this.Close(); Form3 frm = new Form3(); frm.ShowDialog(); } else { //Take each character in received string put in array of char. for (int i = 0; i < endPos; i++) { ch[i] = str.ElementAt(i); } //Take array of chars, put in string temporaryString = new String(ch); //Place all characters button in an array Button[] arrbtn = new Button[10] { button1, button2, button3, button4, button5, button6, button7, button8, button9, button10 }; //Place characters in each button for (int i = 0; i < (arrbtn.Length); i++) { arrbtn[i].Text = temporaryString.ElementAt(i).ToString(); } } } } catch (Exception ex) { MessageBox.Show(ex.Message + "\n" + ex.StackTrace + "\n" + ex.HelpLink + "\n" + ex.InnerException + "\n" + ex.Source + "\n" + ex.TargetSite); } } socketReceive.Close(); control = 0; this.Dispose(true); //Check is last round reached if (GlobalClient.roundPlayed < 4) { //Reopen Form2 until last round is reached GlobalClient.roundPlayed++; Form2 frm = new Form2(); frm.Player1ScoreLabel.Text = GlobalClient.player1score; frm.Player2ScoreLabel.Text = GlobalClient.player2score; if (GlobalClient.roundPlayed == 4) { frm.btnNewRound.Text = "End Game!"; } frm.ShowDialog(); } else { Form3 frm = new Form3(); frm.ShowDialog(); } this.Close(); }